Why is const reference parameter compatible to value parameter?

I have this code:

1
2
3
4
5
6
7
8
void f_source(const int& val)
{}

void CopyFn()
{
	std::function<void(const int&)> fn_source = f_source;
	std::function<void(int)> fn = f_source;
}


Why is the last assignment inside CopyFn() legal C++? f_source takes a const reference to int while the fn object takes an int by value...

Seems to me they are different and should not compile.

Regards,
Juan
Because there is no reason not to allow it. When f_source's operator() receives an int, by value, there is nothing preventing it from passing that int as argument to a function with a const int& parameter. The return and parameter types doesn't have to match exactly as long as they are compatible.
Topic archived. No new replies allowed.