6
考虑下面的代码:Lambda捕获导致不兼容的操作数类型错误?
main()
{
bool t;
...
std::function<bool (bool)> f = t ? [](bool b) { return b; } : [](bool b) { return !b; }; // OK
std::function<bool (bool)> f = t ? [t](bool b) { return t == b; } : [t](bool b) { return t != b; }; // error
}
当铿锵3.1编译的,非捕获拉姆达的分配工作,而一个与捕获失败:
main.cpp:12:36: error: incompatible operand types ('<lambda at main.cpp:12:38>' and '<lambda at main.cpp:12:71>')
std::function<bool (bool)> f2 = t ? [t](bool b) { return t == b; } : [t](bool b) { return t != b; }; // error
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
为什么捕捉相同的变量将导致2个lambda需要不兼容的类型?
感谢您的详细解释。我不知道他们的实施方式不同。现在有道理。 – 2012-07-10 12:24:47