C++0x rvalue references and temporaries

后端 未结 6 1998
独厮守ぢ
独厮守ぢ 2021-02-01 23:37

(I asked a variation of this question on comp.std.c++ but didn\'t get an answer.)

Why does the call to f(arg) in this code call the const ref overload of

相关标签:
6条回答
  • 2021-02-02 00:00

    GCC is doing it wrong according the FCD. The FCD says at 8.5.3 about reference binding

    • If the reference is an lvalue reference and the initializer expression is an [lvalue / class type] ...
    • Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference and the initializer expression shall be an rvalue or have a function type.

    Your case for the call to the std::string && matches none of them, because the initializer is an lvalue. It doesn't get to the place to create a temporary rvalue, because that toplevel bullet already requires an rvalue.

    Now, overload resolution doesn't directly use reference binding to see whether there exist an implicit conversion sequence. Instead, it says at 13.3.3.1.4/2

    When a parameter of reference type is not bound directly to an argument expression, the conversion sequence is the one required to convert the argument expression to the underlying type of the reference according to 13.3.3.1.

    Thus, overload resolution figures out a winner, even though that winner may actually not be able to bind to that argument. For example:

    struct B { B(int) { /* ... */ } };
    struct A { int bits: 1; };
    
    void f(int&);
    void f(B);
    int main() { A a; f(a.bits); }
    

    Reference binding at 8.5 forbids bitfields to bind to lvalue references. But overload resolution says that the conversion sequence is the one converting to int, thus succeeding even though when the call is made later, the call is ill-formed. Thus my bitfields example is ill-formed. If it was to choose the B version, it would have succeeded, but needed a user defined conversion.

    However, there exist two exceptions for that rule. These are

    Except for an implicit object parameter, for which see 13.3.1, a standard conversion sequence cannot be formed if it requires binding an lvalue reference to non-const to an rvalue or binding an rvalue reference to an lvalue.

    Thus, the following call is valid:

    struct B { B(int) { /* ... */ } };
    struct A { int bits: 1; };
    
    void f(int&); /* binding an lvalue ref to non-const to rvalue! */
    void f(B);
    int main() { A a; f(1); }
    

    And thus, your example calls the const T& version

    void f(const std::string &);
    void f(std::string &&); // would bind to lvalue!
    
    void g(const char * arg) { f(arg); }
    

    However, if you say f(arg + 0), you create an rvalue, and thus the second function is viable.

    0 讨论(0)
  • 2021-02-02 00:03

    I don't know if that has changed in the latest versions of the standard, but it used to say something like "if in doubt, don't use the rvalue reference". Probably for compatibility reasons.

    If you want the move semantics, use f(std::move(arg)), that works with both compilers.

    0 讨论(0)
  • 2021-02-02 00:07

    It was a defect in the standard draft you read. This defect got in as a side effect of some eager editing to disallow binding of rvalue references to lvalues for safety reasons.

    Your intuition is right. Of course, there is no harm in allowing an rvalue reference to refer to some unnamed temporary even if the initializer was an lvalue expression. After all, this is what rvalue references are for. The issue you observed has been fixed last year. The upcoming standard will mandate that the second overload will be picked in your example where the rvalue reference will refer to some temporary string object.

    The rule fix made it into the draft n3225.pdf (2010-11-27):

    • [...]
    • Otherwise, the reference shall be an lvalue reference to a non-volatile const type (i.e., cv1 shall be const), or the reference shall be an rvalue reference and the initializer expression shall be an rvalue or have a function type. [...]
      • [...]
      • Otherwise, a temporary of [...] is created [...]
            double&& rrd3 = i; // rrd3 refers to temporary with value 2.0
    

    But N3225 seems to have missed to say what i is in this example. The latest draft N3290 contains these examples:

            double d2 = 1.0;
            double&& rrd2 = d2; // error: copying lvalue of related type
            int i3 = 2;
            double&& rrd3 = i3; // rrd3 refers to temporary with value 2.0
    

    Since your MSVC version was released before this issue got fixed, it still handles rvalue references according to the old rules. The next MSVC version is expected to implement the new rvalue reference rules (dubbed "rvalue references 2.1" by MSVC developers) see link.

    0 讨论(0)
  • 2021-02-02 00:18

    Take a look at this:

    http://blogs.msdn.com/vcblog/archive/2009/02/03/rvalue-references-c-0x-features-in-vc10-part-2.aspx

    rvalue references: overload resolution

    It looks like your case is: "Lvalues strongly prefer binding to lvalue references".

    0 讨论(0)
  • 2021-02-02 00:24

    I did not see the behavior mentioned by Doug on g++. g++ 4.5 and 4.4.3 both call f(string &&) as expected but VS2010 calls f(const string &). Which g++ version are you using?

    0 讨论(0)
  • 2021-02-02 00:24

    A lot of things in the current draft of the standard need clarification, if you ask me. And the compilers are still developing, so it's hard to trust their help.

    It looks pretty clear that your intuition is right… temporaries of any kind are supposed to bind to rvalue references. For example, §3.10, the new "taxonomy" section, categorically defines temporaries as rvalues.

    The problem may be that the RR argument specification is insufficient to invoke the creation of a temporary. §5.2.2/5: "Where a parameter is of const reference type a temporary object is introduced if needed." That sounds suspiciously exclusive.

    Seems to slip through the cracks again at §13.3.3.1/6: (emphasis mine)

    When the parameter type is not a reference, the implicit conversion sequence models a copy-initialization of the parameter from the argument expression. The implicit conversion sequence is the one required to convert the argument expression to a prvalue of the type of the parameter.

    Note that copy-initialization string &&rr = "hello"; works fine in GCC.

    EDIT: Actually the problem doesn't exist on my version of GCC. I'm still trying to figure out how the second standard conversion of the user-defined conversion sequence relates to forming an rvalue reference. (Is RR formation a conversion at all? Or is it dictated by scattered tidbits like 5.2.2/5?)

    0 讨论(0)
提交回复
热议问题