Consider this code:
#include <iostream>
void f(int&& i)
{
std::cout << "f(int&&)\n";
}
void f(const int&& i)
{
std::cout << "f(const int&&)\n";
}
int fun_i()
{
return 0;
}
const int fun_ci()
{
return 0;
}
int main()
{
f(fun_i());
f(fun_ci());
}
If I compile this with MSVC 2012, the output is:
f(int&&)
f(const int&&)
If I compile with GCC 4.7, the output is:
f(int&&)
f(int&&)
Which is correct?
(If I remove the second definition of f, the program will not compile under MSVC 2012, but it does compile under GCC 4.7.)
GCC is correct. From paragraph 4 of 3.10 Lvalues and rvalues [basic.lval]:
Class prvalues can have cv-qualified types; non-class prvalues always have cv-unqualified types. [...]
A function call such as fun_ci()
is in fact a prvalue*, and as such has type int
, not const int
. int&&
is a better match than const int&&
, and should be picked by overload resolution.
*: it's customarily said that top-level cv-qualifiers are ignored for non-class return types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With