void f(int & i){
cout << "l-value-ref" << endl;
}
void f(int && i){
cout << "r-value-ref" << endl;
}
Assuming the code above, we have a overloaded function which takes respectively l-value-reference and r-value-reference parameters.
int x = 5;
f(x);
f(5);
const int j = 9;
f(j);
when i use const int j = 9 compiler gives ambiguity error. How can i solve this problem?
f()
to take aconst int &i
parameter. – Phoxj
which is of typeint const
. The compiler should give "no function found which acceptsint const &
". – Masonvoid f(const int &)
overload. Depends on what you want to achieve. – Fuzzyconst
parameters am i right? – Zymoconst int
argument.\ – Shortwaveerror: binding 'const int' to reference of type 'int&' discards qualifiers
error. So the only solution is to create aconst int &
function. – Zymo