10

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?

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 3
    Isn't there an implicit conversion from `char` to `int` (coming from C I'd expect this)? –  Oct 14 '12 at 10:16
  • @H2CO3: in C++ you can overload on `int` versus `char` as argument type. so, no, there isn't a conversion in this context. or, i fail to see how it can be (correctly) invoked. – Cheers and hth. - Alf Oct 14 '12 at 10:19
  • I know it can be overloaded, but I just thought that implicit conversion may occur here since there's no actual overloading. And also @Maciej Piechotka's answer seems to got an upvote, essentially stating the same thing :) –  Oct 14 '12 at 10:21
  • 2
    @Cheersandhth.-Alf: You can overload but you still have automatic casting. Ie. if you had `void function(char c) {}` it would be colled instead but you construct `Boxed` from `int &&` which you construct from casted `char` (legal as it is r-value, but it wouldn't be if you needed l-value). – Maciej Piechotka Oct 14 '12 at 10:22
  • 2
    Anyway, I can't imagine why this question got a downvote... –  Oct 14 '12 at 10:24
  • 1
    @H2CO3: i'm followed by some serial downvoter. he or she restricts it to sufficiently low frequency that it isn't automatically fixed by SO mechanism. it's somewhat compensated by the occasional serial upvoter. :-) – Cheers and hth. - Alf Oct 14 '12 at 10:27
  • @Cheersandhth.-Alf that's annoying, happens to me, too. :( Lemme give you +1. –  Oct 14 '12 at 10:28
  • just data item: @abyx pointed out in the c++ chat that the above works also for binding the `char` variable to a `double&&`!, http://liveworkspace.org/code/c8763b23d0413ba128ff0402a388d364 – Cheers and hth. - Alf Oct 14 '12 at 10:30
  • @Cheersandhth.-Alf better a serial downvoter than a serial killer – Jonas Schäfer Oct 14 '12 at 10:36

3 Answers3

2

I haven't check the spec but I guess char can be automatically cast to int. Since you cannot assign anything (it's r-value) the R-value to temporary variable of type int (to be more explicit to (int)c value) will be passed.

Maciej Piechotka
  • 7,028
  • 6
  • 39
  • 61
  • Since this answer I updated the question to include an example with `double&&`. `char` -> `int` is a conversion of a different kind (a different set of rules in the standard) than `char` -> `double`. So I think this new example clarifies things a bit: the mechanism can't be the automatic up-conversion to `int`. – Cheers and hth. - Alf Oct 14 '12 at 10:48
  • 1
    @Cheersandhth.-Alf: But conversion `char` -> `double` is still legal so value of type `(double)c` is still a valid r-value. – Maciej Piechotka Oct 14 '12 at 11:43
2

I discovered that N3290 (identical to C++11 standard) contains non-normative example of binding double&& to rvalue generated from int lvalue, and the updated wording in §8.5.3

“If T1 is reference-related to T2 and the reference is an rvalue reference, the initializer expression shall not be an lvalue.”

The rules were reportedly designed to avoid inefficient extra copying. Although I fail to see how such copying could not be optimized away. Anyway, whether the rationale is reasonable or not – and it certainly doesn't seem as a reasonable effect! – the following code is allowed, and compiles with both MSVC 11 and MinGW g++ 4.7:

struct Foo {};
struct Bar { Bar( Foo ) {} };

void ugh( Bar&& ) {}

int main()
{
    Foo o;
    ugh( o );
}

So apparently MSVC 11 is wrong in not permitting the lvalue -> rvalue conversion.


EDIT: I learned that there is Defect Report about this issue, DR 1414. The Feb 2012 conclusion was that the current behavior specification is “correct”, presumably with respect to how well it reflects the intention. It is however reportedly still discussed in the committee, presumably with respect to the practicality of the intention.
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • 1) N3290 _is_ the C++11 standard, it's the paper that got voted on and republished by national bodies as the standard. – Jonathan Wakely Oct 14 '12 at 15:30
  • 2) if `Bar b = o;` is OK, why shouldn't `ugh( o )` also be OK? You get an implicit conversion to a temporary `Bar` and an rvalue reference can bind to the temporary. The first is equivalent to `Bar b = Bar(o)` and the second is equivalent to `ugh( Bar(o) )` except that implicit conversions are allowed, and `Foo` -> `Bar` can be converted implicitly, as can `char` -> `double` – Jonathan Wakely Oct 14 '12 at 15:31
  • @Jonathan: re your "why", you're asking for the rationale of the standard. for ordinary references the main rational was that it would break expectations about changed value after a call. with rvalue references there is still this expectation esp. for use of incoming argument. there is also the expectation of being able to discriminate on type. thus, the same rationale is present. it has resulted in different rules, which logically means that *if the process leading to the rules was perfect*, then some additional concerns for rvalues must have been at play, such as avoiding needless copying. – Cheers and hth. - Alf Oct 14 '12 at 17:22
  • Re (1) thanks, I confused things on new laptop. This was highest numbered draft here and I didn't remember the number. Thanks, fixed! – Cheers and hth. - Alf Oct 14 '12 at 17:41
0

Presumably you agree this is valid?

void foo( double ) {}  // pass-by-value

int main()
{
    char ch = '!';
    foo( ch );
}

There's an implicit conversion from char to double, 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:

void foo( double&& ) {}  // pass-by-reference

int main()
{
    char ch = '!';
    foo( double(ch) );
}

but that doesn't really change anything in this case. That would be necessary if double -> char could only be converted explicitly (e.g. for class types with explicit constructors or explicit conversion operators) but double to char is a valid implicit conversion.

The "an rvalue-reference cannot bind to an lvalue" rule you're thinking of refers to binding a T&& to a T lvalue, and that rule isn't broken because the double&& doesn't bind to the char, 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:

std::vector<std::string> v;
v.push_back("text");

With the current rules a temporary std::string is created by an implicit conversion, then std::vector<T>::push_back(T&&) is called, and the temporary is moved into the vector. If that push_back overload Wasn't viable for the result of a conversion then the code above would call std::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:

v.push_back( std::string{"text"} );

IMHO it makes no sense to have to explicitly construct a std::string when that constructor is not explicit. I want consistent behaviour from explicit/implicit constructors, and I want the first push_back example to be more efficient.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Thanks for the link. It describes the historical reason why rvalue references were prohibited from binding to lvalues; SO readers should beware that the "concepts" that the paper talks about, didn't make it into C++11. However, while interesting on its own, that paper has nothing to do with why the creation of an intermediate rvalue was actively supported. I can't see any good reason for it. This question exemplified one good reason against it (and in general, the reasons against are the same as for ordinary references: expectations about changes, and type discrimination). – Cheers and hth. - Alf Oct 14 '12 at 17:32
  • It doesn't show a reason against it, there's nothing _wrong_ with the code in your question, it just doesn't do what you expected. If `ugh(Bar(o)` is valid, and the conversion `Bar(o)` can be done implicitly in every other context, then for consistency `ugh(o)` should also be valid – Jonathan Wakely Oct 14 '12 at 18:53
  • thank you for your input. i think maybe we're talking at cross purposes here. i am informed from a reliable source (compiler dev team) that the c++ committee is considering this particular issue; i'm sorry i don't have a DR number. so it's certainly open for, uh, argument... ;-) – Cheers and hth. - Alf Oct 14 '12 at 19:26
  • see also http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51066 (where I specifically stated that I don't think there should be a warning for your example, only when initializing a local variable) – Jonathan Wakely Oct 14 '12 at 20:45