[英]Variable cannot be implicitly captured in a lambda with no capture-default specified
I'm following this guy's blog post on C++ Lambdas http://cpptruths.blogspot.com/2014/03/fun-with-lambdas-c14-style-part-1.html and while compiling his code, I ran into a compiler error: 我正在关注这篇关于C ++ Lambdas http://cpptruths.blogspot.com/2014/03/fun-with-lambdas-c14-style-part-1.html的博文,在编写代码时,我遇到了一个编译器错误:
variable 'unit' cannot be implicitly captured in a lambda with no capture-default specified"
The lines that it is referencing are below: 它引用的行如下:
auto unit = [](auto x) {
return [=](){ return x; };
};
auto stringify = [](auto x) {
stringstream ss;
ss << x;
return unit(ss.str());
};
The guy seems like he knows about the new Lambda features in C++, which I certainly do not, which is why I am here now. 这家伙似乎知道C ++中新的Lambda功能,我当然不知道,这就是我现在在这里的原因。 Can someone shed some light on this problem? 有人能解释一下这个问题吗? What do I need to do to get this code compiling correctly? 我需要做什么才能正确编译此代码?
Thanks in advance! 提前致谢! :) :)
edit: Turns out the problem wasn't with unit or stringify. 编辑:原来问题不在于unit或stringify。 Let me paste the new code: 让我粘贴新代码:
auto unit = [](auto x) {
return [=](){ return x; };
};
auto stringify = [unit](auto x) {
stringstream ss;
ss << x;
return unit(ss.str());
};
auto bind = [](auto u) {
return [=](auto callback) {
return callback(u());
};
};
cout << "Left identity: " << stringify(15)() << "==" << bind(unit(15))(stringify)() << endl;
cout << "Right identity: " << stringify(5)() << "==" << bind(stringify(5))(unit)() << endl;
//cout << "Left identity: " << stringify(15)() << endl;
//cout << "Right identity: " << stringify(5)() << endl;
Ok, so, the call to "bind" is what is causing the following error: 好的,所以,对“绑定”的调用是导致以下错误的原因:
"__ZZZ4mainENK4$_17clINSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEDaT_ENUlvE_C1ERKSA_", referenced from:
std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > main::$_19::operator()<auto main::$_17::operator()<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) const::'lambda'()>(auto main::$_17::operator()<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >) const::'lambda'()) const in funwithlambdas-0f8fc6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [funwithlambdas] Error 1
I'll play around with it a bit more and if I fix it I will post here, but please post your solution or comment if you are able to. 我会更多地玩它,如果我修复它,我会在这里发布,但如果你能够发布你的解决方案或评论。 Thanks again! 再次感谢!
There you go: 你去:
auto unit = [](auto x) {
return [=](){ return x; };
};
auto stringify = [unit](auto x) { // or '&unit
stringstream ss;
ss << x;
return unit(ss.str());
};
Lambdas don't capture anything of outer-scope, you must specify it. Lambda不捕获任何外部范围,您必须指定它。
EDIT: User 'TC' is right - both of the lambdas are global in that article. 编辑:用户'TC'是对的 - 这篇文章中的两个lambdas都是全局的。 In that case unit
gets accessed without specification. 在这种情况下,无需指定即可访问unit
。 And with specification (which I gave), it fails in VC2015. 并且随着规范(我给出),它在VC2015中失败了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.