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.
barinsidefoo()? If not, just useBar const * bar(same asconst Bar *). A const pointer means that the data is const, not the pointer itself.foois defined asfoo(Bar const * const bar)then you cannot use thebarcaptured by the lambda to modify whatever thebarpassed tofoopoints to.[bar] (const Bar* element)for the lambda andvoid foo(const Bar* bar). Now both are const.