【发布时间】:2014-07-09 13:52:25
【问题描述】:
让这段代码讲述故事(或观看showterm):
#include <iostream>
int foo(bool func (void)) {
int i; for (i = 0; i < 10 && func(); i++);
return i;
}
int main() {
std::cout << foo([] {
return true;
}) << std::endl;
bool a = false;
std::cout << foo([&a] { // error: no matching function for call to 'foo'
return a = !a;
}) << std::endl;
return 0;
}
我希望能够在我的 lambda 中捕获 a,并能够交替返回值。
我的实际案例涉及更多,但归结为这一点。我希望能够使用 lambda,尽管替代方法是使用带有全局变量的普通函数来保存状态。
我正在编译:
clang++ -std=c++11 testcase.cc
我正在使用 Apple 的 LLVM:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
这是一个错误,还是我做错了什么?
【问题讨论】:
标签: c++ c++11 lambda clang llvm