Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing pointers in lambda expression?

Tags:

c++

c++11

lambda

I have a function that uses a lambda expression.

std::vector<Bar*> mBars;

void foo(Bar* bar)
{
    auto duplicateBars = std::remove_if(mBars.begin(), mBars.end(),
        [bar] (const Bar* const &element)
        {
            return bar == element;
        });

    mBars.erase(duplicateBars, mBars.end());
}

Later, I reviewed the code and realized I could add two consts to foo's signature.

void foo(const Bar* const bar);

bar's pointer and data is now constant, but for the purpose of the lambda expression the pointer itself is constant, because I captured by value. However, the data pointed to can be changed and there is no way to change this, because const is not allowed in the lambda capture.

This is unintuitive to me. Is my interpretation correct? I can use the second signature, but I cannot protect the data from being changed in the lambda expression.

like image 946
user870130 Avatar asked May 28 '14 18:05

user870130


People also ask

Does lambda capture this pointer?

Lambda expressions declared within a non-static member function explicilty or implicitly captures the this pointer to access to member variables of this.

What is capture in lambda expression?

Capture clauseVariables that have the ampersand ( & ) prefix are accessed by reference and variables that don't have it are accessed by value. An empty capture clause, [ ] , indicates that the body of the lambda expression accesses no variables in the enclosing scope.

What is capture clause in lambda C++?

A capture clause of lambda definition is used to specify which variables are captured and whether they are captured by reference or by value. An empty capture closure [ ], indicates that no variables are used by lambda which means it can only access variables that are local to it.

Are lambdas function pointers?

Lambda expressions, even captured ones, can be handled as a function pointer (pointer to member function). It is tricky because an lambda expression is not a simple function. It is actually an object with an operator().


2 Answers

However, the data pointed to can be changed and there is no way to change this, because const is not allowed in the lambda capture.

No, when capturing by value in a lambda expression constness is preserved, i.e. capturing a pointer to const data will prevent changes to the data inside the lambda.

int i = 1;
const int* ptr = &i;

auto func = [ptr] {
    ++*ptr; // ERROR, ptr is pointer to const data.
}

A lambda will also add top-level constness to pointers when capturing by value (unless using mutable).

auto func = [ptr] {
    ptr = nullptr; // ERROR, ptr is const pointer (const int* const).
}

auto func = [ptr] () mutable { // Mutable, will not add top-level const.
    ptr = nullptr; // OK
}

I can use the second signature, but I cannot protect the data from being changed in the lambda expression.

You can protect the data from being changed inside the lambda by using const.

const Bar* bar = &bar_data;
auto b = [bar] (const Bar* element) { // Data pointed to by bar is read-only.
    return bar == element;
};

Also the lambda expression takes a parameter of type const Bar* const &, i.e. reference to const pointer to const data. No need to take a reference, simply take a const Bar*.

More info about pointers and const: What is the difference between const int*, const int * const, and int const *?

like image 70
Felix Glas Avatar answered Sep 21 '22 02:09

Felix Glas


Your question seems to arise from a misunderstanding of how capturing of variables in lambda expressions works. When you capture a variable by copy, the corresponding data member created in the closure type generated from the lambda expression will have the same type as the original object. This preserves const-ness, and you cannot go modify whatever bar points to within the body of the lambda.

From §5.1.2/15 [expr.prim.lambda]

An entity is captured by copy if it is implicitly captured and the capture-default is = or if it is explicitly captured with a capture that is not of the form & identifier or & identifier initializer. For each entity captured by copy, an unnamed non-static data member is declared in the closure type. The declaration order of these members is unspecified. The type of such a data member is the type of the corresponding captured entity if the entity is not a reference to an object, or the referenced type otherwise.

Live demo

like image 20
Praetorian Avatar answered Sep 23 '22 02:09

Praetorian