Actually there is a way to solve this problem without changing the variable or parameter name, by aliasing the capture:
std::function<int(int)> functionPointer = [b=a](int a)-> int{ return a + b + 100 ; };
As explained here, since C++14 lambda capture is generalized, which includes renaming variables of the lambda object. While a capture [a]
would copy local variable a
from outer scope into a lambda object variable a
, we can also name that lambda object variable with the syntax [b=a]
, so the lambda's copy is known as b
. We can also define lambda object variables with expressions, e.g. [a=5*a]
or [b=5*a]
, which is sometimes a neat way of e.g. passing members of a struct or results of accessor methods to a lambda.
It allows to do things previously not possible, namely passing a unique_ptr
to the lambda object (i.e. transferring ownership!) with std::move()
.