简体   繁体   English

在没有指定capture-default的lambda中无法隐式捕获变量

[英]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.

相关问题 变量不能在 lambda 中隐式捕获,而没有使用 switch 语句指定捕获默认值 - variable cannot be implicitly captured in a lambda with no capture-default specified using a switch statement C++ Lambda 谓词原因变量不能在 lambda 中隐式捕获,但未指定捕获默认值 - C++ Lambda Predicate Cause Variable Canot be implicitly captured in a lambda with no capture-default specified 在未指定捕获默认值的模板 lambda 中隐式捕获 const 变量 - Implicitly capture const variable in a template lambda with no capture-default specified 错误:变量“无法隐式捕获,因为未指定默认捕获模式” - Error: variable “cannot be implicitly captured because no default capture mode has been specified” 编译器错误C3493:无法隐式捕获&#39;func&#39;,因为未指定默认捕获模式 - Compiler error C3493: 'func' cannot be implicitly captured because no default capture mode has been specified 错误C3493:无法隐式捕获残差&#39;,因为未指定默认捕获模式 - Error C3493: residual' cannot be implicitly captured because no default capture mode has been specified 不能在模板 function 中隐式捕获变量 - Variable cannot be implicitly captured in template function c ++ lambda,无法访问由副本捕获的变量 - c++ lambda, cannot access variable captured by copy 在lambda中使用未捕获的变量 - Using of not captured variable in lambda 编译错误:在此上下文中无法隐式捕获“this” - Compilation error : 'this' cannot be implicitly captured in this context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM