Capturing pointers in lambda expression?

18,240

Solution 1

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 *?

Solution 2

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

Share:
18,240
user870130
Author by

user870130

Updated on June 15, 2022

Comments

  • user870130
    user870130 almost 2 years

    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.

  • user870130
    user870130 almost 10 years
    Could you explain your last point, "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*."
  • Felix Glas
    Felix Glas almost 10 years
    @user870130 Passing a reference to a pointer is only useful if you intend to modify the pointer passed to the lambda from inside the lambda. In your case you only check pointer equality so const Bar* is enough.