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]:
A function call such as
fun_ci()is in fact a prvalue*, and as such has typeint, notconst int.int&&is a better match thanconst int&&, and should be picked by overload resolution.*: it’s customarily said that top-level cv-qualifiers are ignored for non-class return types.