2

In node.js I can write lambda inside lambda and capture whichever variable I want. But in C++11, as lambda functions are actually functor object, variable capturing is not so easy with nested lambdas.

I'm using [this] to capture this pointer so I can use class members. But inside a lambda, this pointer points to the lambda instead of the outter class.

void MyClass::myFunction() {
    // call the lambda1
    connect(action, trigger, [this]() {
        // in the lambda1, call lambda2
        sendCommand([this]() {      // <-- I want `this` point to the outter class
            this->myMember.log("something");  // myMember is the member of class MyClass
        });
    });
}

I know it can be done by rename this to another pointer variable and capture that variable instead of this, but I think that way is ugly.

Is there any better way to capture outter this?

3
  • @BЈовић Why would capturing this implicitly by copy be in any way "better" than capturing it explicitly by copy?
    – Casey
    Jun 24, 2014 at 14:15
  • @Casey My mistake. I really meant [&] Jun 24, 2014 at 16:01
  • 1
    @BЈовић It doesn't make sense to capture this by reference since it is a prvalue. Indeed, the standard does not allow explicitly capturing this by reference: both [&this] and [=,this] are explicitly forbidden. Allowing implicit capture of this by reference is probably an error in the standard. Compilers treat implicit reference capture as capture by copy. (See restriction about default capture mode and 'this' in C++ lambda-expression for more discussion.)
    – Casey
    Jun 24, 2014 at 16:19

2 Answers 2

3

You simply capture outer lambda's context:

#include <iostream>

struct Omg
{
  int lol= 42;

  Omg()
  {
    [this]{ [&]{ std::cout<< this-> lol; }(); }();
  }
};

int main(){ Omg(); }
3

But inside a lambda, this pointer points to the lambda instead of the outter class.

No, inside the lambda this has the same value as outside. The only problem in your code is accessing this with . instead of ->. This program:

void MyClass::myFunction() {
    std::cout << this << std::endl;
    // call the lambda1
    connect(action, trigger, [this]() {
        std::cout << this << std::endl;
        // in the lambda1, call lambda2
        sendCommand([this]() {      // <-- I want `this` point to the outter class
            std::cout << this << std::endl;
            this->myMember.log("something");  // myMember is the member of class MyClass
        });
    });
}

Prints the same value for this in all three places:

g++ -std=c++11 -O3 -Wall -Wextra -pedantic -pthread main.cpp && ./a.out
0x7fff4286c80f
0x7fff4286c80f
0x7fff4286c80f

N3936 (C++14 Working Draft) [expr.prim.lambda]/18 states:

Every id-expression within the compound-statement of a lambda-expression that is an odr-use (3.2) of an entity captured by copy is transformed into an access to the corresponding unnamed data member of the closure type. [ Note: An id-expression that is not an odr-use refers to the original entity, never to a member of the closure type. Furthermore, such an id-expression does not cause the implicit capture of the entity. —end note ] If this is captured, each odr-use of this is transformed into an access to the corresponding unnamed data member of the closure type, cast (5.4) to the type of this. [ Note: The cast ensures that the transformed expression is a prvalue. —end note ]

3
  • Sorry I made a mistake in the code above (I've fixed it, now this-> instead of this.). Jun 24, 2014 at 14:47
  • I tried your method but failed. I printed out two different value of this: ffffe190 and ffffba53. I'm doing this in Qt and use std::function<> as the type of callback. Jun 24, 2014 at 14:59
  • @songziming Sounds like a compiler bug. Are you by any chance using VS2010?
    – Casey
    Jun 24, 2014 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.