Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

For the following code:

struct B
{
    void g()
    {
        []() { B::f(); }();
    }

    static void f();
};

g++ 4.6 gives the error:

test.cpp: In lambda function:
test.cpp:44:21: error: 'this' was not captured for this lambda function

(Interestingly, g++ 4.5 compiles the code fine).

Is this a bug in g++ 4.6, or is it really necessary to capture the 'this' parameter to be able to call a static member function? I don't see why it should be, I even qualified the call with B::.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
206 views
Welcome To Ask or Share your Answers For Others

1 Answer

I agree, it should compile just fine. For the fix (if you didn't know already), just add the reference capture and it will compile fine on gcc 4.6

struct B
{
    void g()
    {
        [&]() { B::f(); }();
    }

    static void f() { std::cout << "Hello World" << std::endl; };
};

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...