...
In this compliant solution, the std::function
object returns an int
instead of a const int &
, ensuring that the value is copied instead of bound to a temporary reference. An alternative solution would be to call the lambda directly instead of through the std::function
objectfunction<>
object.
Code Block | ||||
---|---|---|---|---|
| ||||
#include <functional> void f() { auto l = [](const int &j) { return j; }; std::function<int(const int &)> fn(l); int i = 42; int j = fn(i); } |
...