Variable cannot be implicitly captured in a lambda without a default value to capture

I am following this blog post in C ++ Lambdas http://cpptruths.blogspot.com/2014/03/fun-with-lambdas-c14-style-part-1.html and while compiling its code I came across compiler error:

variable 'unit' cannot be implicitly captured in a lambda with no capture-default specified"

      

The lines he links to are below:

auto unit = [](auto x) {
    return [=](){ return x; };
};
auto stringify = [](auto x) {
    stringstream ss;
    ss << x;
    return unit(ss.str());
};

      

The guy seems to know about the new Lambda features in C ++, which I certainly don't, which is why I'm here now. Can someone shed some light on this issue? What do I need to do to handle this code correctly?

Thanks in advance!:)


edit: Turns out the problem is not device or string related. Let me insert some 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;

      

So the call to "bind" is what causes 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 little more and if I fix it I'll post it here, but please post your solution or comment if you can. Thanks again!

+3


source to share


1 answer


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 does not grab anything external, you must specify it.

EDIT: TC 'user is correct - both lambdas are global in this article. In this case, it unit

gets access without specification. And with the spec (which I gave), it fails in VC2015.

+1


source







All Articles