Title
Direct binding of references to non-class rvalue references
Status
cd2
Section
9.4.4 [dcl.init.ref]
Submitter
Eric Niebler

Created on 2007-12-01.00:00:00 last changed 178 months ago

Messages

Date: 2010-03-15.00:00:00

[Voted into WP at March, 2010 meeting as document N3055.]

Date: 2010-02-15.00:00:00

Proposed resolution (February, 2010):

See paper N3030.

Date: 2008-02-15.00:00:00

Notes from the February, 2008 meeting:

You can't just treat scalar rvalues like class and array rvalues, because they might not have an associated object. However, if you have an rvalue reference, you know that there is an object, so probably the best way to address this issue is to specify somehow that binding a reference to an rvalue reference does not introduce a new temporary.

(See also issues 690 and 846.)

Date: 2007-12-01.00:00:00

According to 9.4.4 [dcl.init.ref] paragraph 5, a reference initialized with a reference-compatible rvalue of class type binds directly to the object. A reference-compatible non-class rvalue reference, however, is first copied to a temporary and the reference binds to that temporary, not to the target of the rvalue reference. This can cause problems when the result of a forwarding function is used in such a way that the address of the result is captured. For example:

    struct ref {
        explicit ref(int&& i): p(&i) { }
        int* p;
    };

    int&& forward(int&& i) {
        return i;
    }

    void f(int&& i) {
        ref r(forward(i));
        // Here r.p is a dangling pointer, pointing to a defunct int temporary
    }

A formulation is needed so that rvalue references are treated like class and array rvalues.

History
Date User Action Args
2010-03-29 00:00:00adminsetmessages: + msg2712
2010-03-29 00:00:00adminsetstatus: review -> cd2
2010-02-16 00:00:00adminsetmessages: + msg2544
2010-02-16 00:00:00adminsetstatus: drafting -> review
2008-03-17 00:00:00adminsetmessages: + msg1607
2008-03-17 00:00:00adminsetstatus: open -> drafting
2007-12-01 00:00:00admincreate