【发布时间】:2015-08-21 13:36:44
【问题描述】:
当我在 https://stackoverflow.com/a/32115498/383779 上回答我自己的问题时,我又有了疑问。
在
const CArray<CItem*>& Items=
(ItemsInput!= nullptr)?
*ItemsInput
:
[this]() -> const CArray<CItem*>&
{
CArray<CItem*> InnerItems;
GetContainer().GetInnerItems(InnerItems, NULL, true);
return (InnerItems);
} ()
;
我尝试删除-> const CArray<CItem*>&返回部分,但编译时出现两个错误:
1>FunctionClass.cpp(line of last semicolon): error C2440: 'initializing' : cannot convert from 'void' to 'const CArray<TYPE> &'
1> with
1> [
1> TYPE=CItem *
1> ]
1> Expressions of type void cannot be converted to other types
1>FunctionClass.cpp(line of the return statement): error C3499: a lambda that has been specified to have a void return type cannot return a value
有人可以解释为什么吗?不是应该让 lambda 自动从它的 return 语句中推断出要返回的类型吗?
【问题讨论】:
-
您不能返回对局部变量的引用(
const与否)。参见例如this old question and its answers.
标签: c++ lambda return-type type-deduction