There are two factors that make your code work. First, function arguments are allowed up to one implicit conversion, if that allows them to match overload. Secondly, const links can be attached to temporary. What happens here, y implicitly converted to int , creating a temporary copy. v then tied to this temporary.
Consider the following example:
#include <iostream> void foo(const unsigned int & v) { std::cout << &v << '\n'; } void bar(const int & v) { std::cout << &v << '\n'; } int main() { unsigned int y = 1; std::cout << &y << '\n'; foo(y); bar(y); return 0; }
You will find that foo(y) prints the same address as y , where bar(y) prints a different address. This will not work with const links. Remarkably, if this were possible, it would mean that changing v cannot actually change y .
François Andrieux
source share