我的书说:
具有函数体的Lambda包含除了单个return语句之外的任何未指定返回类型的返回void.
但是这个:
auto f = []{
int i=0; i++;
return std::string("foo");
};
std::cout << f() << std::endl;
Run Code Online (Sandbox Code Playgroud)
实际上编译并打印出"foo",但是lambda expr不仅仅包含一个return语句,因此它应该返回void,因为它不会手动指定" - > std :: string"作为返回类型.
这里发生了什么?
我在基于Clang 3.2的最新Xcode 4.6中使用Apple的编译器似乎:
clang --version
Apple LLVM版本4.2(clang-425.0.24)(基于LLVM 3.2svn)目标:x86_64-apple-darwin12.2.0线程模型:posix
据我所知,在标准C++ 11(不是C++ 14)中,当省略lambda的返回类型时,其返回类型推断为:
void 在所有其他情况下.现在考虑这段代码:
#include <iostream>
auto closure = [](int x)
{
x++;
return x;
};
int main()
{
int y = closure(10);
std::cout << y << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
这应该属于案例2.但是代码编译好像是带有auto类型推导的C++ 14 ,在g ++ 4.9.2,g ++ 5和clang ++中都有-pedantic -Wall -Wextra -std=c++11.这里发生了什么?我是否解释了标准错误?
在C++ 14中,为什么lambda函数带有推导的返回类型默认情况下从返回类型中删除引用?IIUC,因为C++ 14 lambda函数具有推导的返回类型(没有显式的尾随返回类型)具有返回类型auto,它会丢弃引用(以及其他内容).
为什么做出这个决定?在我看来,当你的返回语句返回时,就像删除引用一样.
这种行为给我带来了以下令人讨厌的错误:
class Int {
public:
Int(int i) : m_int{i} {}
int m_int;
};
class C {
public:
C(Int obj) : m_obj{obj} {}
const auto& getObj() { return m_obj; }
Int m_obj;
};
class D {
public:
D(std::function<const Int&()> f) : m_f{f} {}
std::function<const Int&()> m_f;
};
Int myint{5};
C c{myint};
D d{ [&c](){ return c.getObj(); } } // The deduced return type of the lambda is Int (with no reference)
const Int& myref …Run Code Online (Sandbox Code Playgroud) 当我在/sf/answers/2248084891/上回答我自己的问题时,我又有了疑问.
在
const CArray<CItem*>& Items=
(ItemsInput!= nullptr)?
*ItemsInput
:
[this]() -> const CArray<CItem*>&
{
CArray<CItem*> InnerItems;
GetContainer().GetInnerItems(InnerItems, NULL, true);
return (InnerItems);
} ()
;
Run Code Online (Sandbox Code Playgroud)
我试图删除-> 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 …Run Code Online (Sandbox Code Playgroud)