Title
Do not allow reinterpret_cast from prvalue to rvalue reference
Status
open
Section
7.6.1.10 [expr.reinterpret.cast]
Submitter
Brian Bi

Created on 2024-10-01.00:00:00 last changed 2 weeks ago

Messages

Date: 2024-10-02.22:28:14

Suggested resolution:

  1. Change in 7.6.1.10 [expr.reinterpret.cast] paragraph 1 as follows:

    The result of the expression reinterpret_cast<T>(v) is the result of converting the expression v to type T. If T is an lvalue reference type or an rvalue reference to function type, the result is an lvalue; if T is an rvalue reference to object type, the result is an xvalue; otherwise, the result is a prvalue and the lvalue-to-rvalue (7.3.2 [conv.lval]), array-to-pointer (7.3.3 [conv.array]), and function-to-pointer (7.3.4 [conv.func]) standard conversions are performed on the expression v. The temporary materialization conversion (7.3.5 [conv.rval]) is not performed on v. Conversions that can be performed explicitly using reinterpret_cast are listed below. No other conversion can be performed explicitly using reinterpret_cast.
  2. Remove 7.6.1.10 [expr.reinterpret.cast] paragraph 3:

    [Note 1: The mapping performed by reinterpret_cast might, or might not, produce a representation different from the original value. —end note]
  3. Remove from 7.6.1.10 [expr.reinterpret.cast] paragraph 6 as follows:

    ... [Note 5: See also 7.3.12 [conv.ptr] for more details of pointer conversions. —end note]
  4. Change in 7.6.1.10 [expr.reinterpret.cast] paragraph 11 as follows:

    A glvalue of type T1, designating an object or function x, can be cast to the type “reference to T2” if an expression of type “pointer to T1” can be explicitly converted to the type “pointer to T2” using a reinterpret_cast. The result is that of *reinterpret_cast<T2 *>(p) where p is a pointer to x of type “pointer to T1”. [ Note: No temporary is created, no copy is made, and no constructors (11.4.5 [class.ctor]) or conversion functions (11.4.8 [class.conv]) are called [ Footnote: ... ]. -- end note ]
Date: 2024-10-01.00:00:00

(From submission #617.)

In 7.6.1.10 [expr.reinterpret.cast] paragraph 11, there is an issue similar to the one described in issue 2879: The operand for a cast to reference type can be a "glvalue of type T1", which implies that a prvalue is also acceptable, because it is materialized per 7.2.1 [basic.lval] paragraph 7. However, no implementation accepts reinterpret_cast<double&&>(0).

History
Date User Action Args
2024-10-02 22:28:14adminsetmessages: + msg7835
2024-10-01 00:00:00admincreate