#include <iostream> #include <functional> using Callback = std::function<void(const int)>; int main() { Callback testCall = [](const int &num) { std::cout << "callback: " << num << " - " << &num << std::endl; }; int num = 42; testCall(num); std::cout << "main: " << num << " - " << &num << std::endl; } Possible output:
callback: 42 - 000000B19197F618 main: 42 - 000000B19197F694 As you can see, even if i assign a lambda function which takes the parameter by reference it still uses a copy.
Is that correct?
If yes, why does it still compile? Why is there at least not a warning about the discrepancy between the Callback declaration parameters and the assigned lambda. (const int &num vs const int num)
When not usingconst it does not compile.
PS. if you find a better title, feel free to edit.
intonCallbackandconst int&on the lambda on purpose? If yes you should mention that in the question. When I first read it i overlooked that detailstd::function<void(const int)>withstd::function<void(const char)>and it would still compile.std::functionis a class used for type-erasure, so as long as calling one signature with another would make sense and is legal it will work.