Comp rvalue compiler difference

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 using MSVC 2012, the output will be as follows:

 f(int&&) f(const int&&) 

If I compile with GCC 4.7, the output will be as follows:

 f(int&&) f(int&&) 

What is right?

(If I delete the second definition of f, the program will not compile in MSVC 2012, but it compiles under GCC 4.7.)

+7
source share
2 answers

GCC is correct. From item 4 of 3.10 Lvalues โ€‹โ€‹and rvalues โ€‹โ€‹[basic.lval]:

The prvalues โ€‹โ€‹class can have cv-quali-fi types; non-class prvalues โ€‹โ€‹always have cv-unqualified types. [...]

A function call, such as fun_ci() , is actually prvalue *, and as such is of type int , not const int . int&& better than const int&& , and should be selected using overload resolution.

*: it was usually said that top-level cv qualifiers are ignored for non-class return types.

+9
source

I am inclined to say that gcc seems to be doing the right thing based on the warning it emits:

 stieber@gatekeeper :~$ g++ -std=c++11 -Wignored-qualifiers Test.cpp Test.cpp:20:18: warning: type qualifiers ignored on function return type [-Wignored-qualifiers] 
+2
source

All Articles