Title
static_cast of temporary to rvalue reference
Status
c++14
Section
7.6.1.9 [expr.static.cast]
Submitter
Michael Wong

Created on 2011-08-17.00:00:00 last changed 114 months ago

Messages

Date: 2014-02-15.00:00:00

[Moved to DR at the February, 2014 meeting.]

Date: 2013-09-15.00:00:00

Proposed resolution (September, 2013):

Change 7.6.1.9 [expr.static.cast] paragraph 3 as follows:

A glvalue, class prvalue, or array prvalue of type “cv1 T1” can be cast to type “rvalue reference to cv2 T2” if “cv2 T2” is reference-compatible with “cv1 T1” (9.4.4 [dcl.init.ref]). If the glvalue value is not a bit-field, the result refers to the object or the specified base class subobject thereof; otherwise, the lvalue-to-rvalue conversion (7.3.2 [conv.lval]) is applied to the bit-field and the resulting prvalue is used as the expression of the static_cast for the remainder of this section. If T2 is an inaccessible (11.8 [class.access]) or ambiguous (6.5.2 [class.member.lookup]) base class of T1, a program that necessitates such a cast is ill-formed.
Date: 2012-02-15.00:00:00

Proposed resolution (February, 2012) [SUPERSEDED]:

Change 7.6.1.9 [expr.static.cast] paragraph 4 as follows:

Otherwise, an expression e can be explicitly converted to a type T using a static_cast of the form static_cast<T>(e) if the declaration T t(e); is well-formed, for some invented temporary variable t (9.4 [dcl.init]). The effect...
Date: 2012-02-15.00:00:00

Notes from the February, 2012 meeting:

The reference is bound to the xvalue result of the static_cast, so the lifetime of the temporary is not extended and this example results in a dangling reference.

Date: 2013-10-14.00:00:00
N3690 comment CA 1

In a declaration like

    T&& r = static_cast<T&&>(T());

it is not clear what the lifetime of the T temporary should be. According to 7.6.1.9 [expr.static.cast] paragraph 4, the static_cast is equivalent to a declaration of an invented temporary variable t. The lifetime of the temporary is extended to that of t, but it is not clear what that lifetime should be, nor if the subsequent binding of t to r would affect the lifetime of the original temporary. (See also issue 1568.)

History
Date User Action Args
2014-11-24 00:00:00adminsetstatus: dr -> c++14
2014-03-03 00:00:00adminsetmessages: + msg4942
2014-03-03 00:00:00adminsetstatus: ready -> dr
2013-10-14 00:00:00adminsetmessages: + msg4582
2013-10-14 00:00:00adminsetstatus: drafting -> ready
2012-11-03 00:00:00adminsetstatus: review -> drafting
2012-02-27 00:00:00adminsetmessages: + msg3751
2012-02-27 00:00:00adminsetmessages: + msg3750
2012-02-27 00:00:00adminsetstatus: open -> review
2011-08-17 00:00:00admincreate