As an answer to another question I wanted to post the following code (that is, I wanted to post code based on this idea):
#include <iostream>
#include <utility> // std::is_same, std::enable_if
using namespace std;
template< class Type >
struct Boxed
{
Type value;
template< class Arg >
Boxed(
Arg const& v,
typename enable_if< is_same< Type, Arg >::value, Arg >::type* = 0
)
: value( v )
{
wcout << "Generic!" << endl;
}
Boxed( Type&& v ): value( move( v ) )
{
wcout << "Rvalue!" << endl;
}
};
void function( Boxed< int > v ) {}
int main()
{
int i = 5;
function( i ); //<- this is acceptable
char c = 'a';
function( c ); //<- I would NOT like this to compile
}
However, while MSVC 11.0 chokes at the last call, as it IHMO should, MinGW g++ 4.7.1 just accepts it, and invokes the constructor with rvalue reference formal argument.
It looks to me as if an lvalue is bound to an rvalue reference. A glib answer could be that the lvalue is converted to rvalue. But the question is, is this a compiler bug, and if it’s not, how does the Holy Standard permit this?
EDIT: I managed to reduce it all to the following pretty short example:
void foo( double&& ) {}
int main()
{
char ch = '!';
foo( ch );
}
Fails to compile with MSVC 11.0, does compile with MinGW 4.7.1, which is right?
Presumably you agree this is valid?
There’s an implicit conversion from
chartodouble, so the function is viable.It’s the same in the example in your edited question, there’s an implicit conversion that produces a temporary (i.e. an rvalue) and the rvalue-reference argument binds to that temporary. You can make that conversion explicit if you prefer:
but that doesn’t really change anything in this case. That would be necessary if
double->charcould only be converted explicitly (e.g. for class types with explicit constructors or explicit conversion operators) butdoubletocharis a valid implicit conversion.The “an rvalue-reference cannot bind to an lvalue” rule you’re thinking of refers to binding a
T&&to aTlvalue, and that rule isn’t broken because thedouble&&doesn’t bind to thechar, it binds to a temporary created by the implicit conversion.That rule doesn’t only exist to prevent unnecessary extra copying, but to fix a real safety problem that existed with the previous rules, see http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2008/n2812.html
Edit: It was asked whether this behaviour is desirable on the committee reflector (see DR 1414) and it was decided that yes, this behaviour is intended and is correct. One of the arguments used to reach that position was that this code is more efficient with the current rules:
With the current rules a temporary
std::stringis created by an implicit conversion, thenstd::vector<T>::push_back(T&&)is called, and the temporary is moved into the vector. If thatpush_backoverload Wasn’t viable for the result of a conversion then the code above would callstd::vector<T>::push_back(const T&)which would cause a copy. The current rules make this real-world use case more efficient. If the rules said rvalue-refs cannot bind to the result of implicit conversions you would have to change the code above to get the efficiency of a move:IMHO it makes no sense to have to explicitly construct a
std::stringwhen that constructor is not explicit. I want consistent behaviour from explicit/implicit constructors, and I want the firstpush_backexample to be more efficient.