让这段代码讲述故事(或观看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;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够a在我的lambda中捕获,并能够交替返回值.我的实际情况涉及更多,但归结为此.我希望能够使用lambdas,尽管替代方法是使用带有全局变量的普通函数来保持状态.
我正在编译:
clang++ -std=c++11 testcase.cc
Run Code Online (Sandbox Code Playgroud)
我正在使用Apple的LLVM:
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM …Run Code Online (Sandbox Code Playgroud)