The type conversion rules are rather complex to explain in general.
But for your example, both parameters are int&
, which means "reference to int
". Since you pass two valid int
variables, it's exactly the type expected and the reference of the variables is passed as argument.
Would you have tried with a different type, it would have failed, for example :
long a=10,b=20;
would have failed to compile since it is not possible to get an int
reference to refer to the non-int original variables.
swap(10,20);
would have failed, because the parameters are literal int
values and not variables. It is not possible to get a reference to such a value.
const int a=10;
would also have failed. This time because the const of the variable is an additional constraint that the parameter passing is not allowed to losen.
Not related: You should include <iostream>
and the output should look like:
std::cout << "A is "<< a << " and B is " << b << std::endl;;