3
#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.

6
  • 3
    is this discrepancy int on Callback and const int& on the lambda on purpose? If yes you should mention that in the question. When I first read it i overlooked that detail Commented Mar 12, 2020 at 9:40
  • @idclev463035818 thats exactly what the question is about Commented Mar 12, 2020 at 9:41
  • 1
    The std function takes a non reference argument parameter Use ` std::function<void(const int&)>` instead, it will work Commented Mar 12, 2020 at 9:44
  • 2
    also, making it more clear in the question will avoid comments that tell you to fix the type of the callback ;) As I understood you are asking whether this is correct behavior (not how to fix the code) Commented Mar 12, 2020 at 9:46
  • 2
    You could replace std::function<void(const int)> with std::function<void(const char)> and it would still compile. std::function is a class used for type-erasure, so as long as calling one signature with another would make sense and is legal it will work. Commented Mar 12, 2020 at 10:05

1 Answer 1

2

This is because testCall is a functor object that catch its parameter by copy and then call the lambda on it.

Try:

Callback f = [](const int &num) { std::cout << "callback: " << num << " - " << &num << std::endl; }; int main() { int num = 999; std::cout << "callback: " << num << " - " << &num << std::endl; f(num); [](const int &num) { std::cout << "callback: " << num << " - " << &num << std::endl; }(num); } 

you will see something like:

callback: 999 - 0x7ffeed60a9bc callback: 999 - 0x7ffeed60a994 callback: 999 - 0x7ffeed60a9bc 

which means that callBack is not the function by itself but an indirection to the function. And there is no problem regarding types...

Answer to this may helps you to understand what happens under the hood: How std::function works

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.