3

Possible Duplicate:
lambdas require capturing 'this' to call static member function?

I want to use c++11 lambda in a non-static member function and call a static member function of same class:

class A {
    static void a() {}
public:
    void x() {
        [] () { A::a(); }();
    }
};

But gcc4.6 and gcc4.7 both got an error: error: 'this' was not captured for this lambda function

Why does lambda need 'this' since 'a' is a static member function.

And if 'x' is static or 'a' is static member function of other class, 'this' is not necessary, why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
sealiay
  • 45
  • 1
  • 4

1 Answers1

1
class A {
    static void a() {}
public:
    void x() {
        [] () { A::a(); }();
    }
};

int main() { A a; a.x(); }

This compiles fine with clang 3.2. Also, there is no reason why this shouldn't be compile. This is a bug in GCC as per the notes. This ought to be fixed in 4.7.1. Note that gcc started early but has lagged behind clang in supporting some of the C++11 features.

Edit: There is a similar SO question which you may want to check out.

Community
  • 1
  • 1
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • Seems it is a gcc's bug and gcc4.7.1 is still wrong. Wish 4.7.2 can fix it. – sealiay Jun 18 '12 at 05:33
  • @sealiay: I think you should post your findings on GCC's bug notes! (Don't forget to mention the platform you're on and other required details.) – dirkgently Jun 18 '12 at 05:35