C++ 0x中的闭包和嵌套lambda

Dan*_*Dan 14 c++ lambda visual-c++-2010 c++11

使用C++ 0x,当我在lambda中有lambda时如何捕获变量?例如:

std::vector<int> c1;
int v = 10; <--- I want to capture this variable

std::for_each(
    c1.begin(),
    c1.end(),
    [v](int num) <--- This is fine...
    {
        std::vector<int> c2;

        std::for_each(
            c2.begin(),
            c2.end(),
            [v](int num) <--- error on this line, how do I recapture v?
            {
                // Do something
            });
    });
Run Code Online (Sandbox Code Playgroud)

Pup*_*ppy 8

std::for_each(
        c1.begin(),
        c1.end(),
        [&](int num)
        {
            std::vector<int> c2;
            int& v_ = v;
            std::for_each(
                c2.begin(),
                c2.end(),
                [&](int num)
                {
                    v_ = num;
                }
            );
        }
    );
Run Code Online (Sandbox Code Playgroud)

不是特别干净,但确实有效.