Movatterモバイル変換


[0]ホーム

URL:


This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118e. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-11-05


1965. Explicit casts to reference types

Section:7.6.1.7  [expr.dynamic.cast]    Status:CD7    Submitter:Richard Smith    Date:2014-07-07

[Resolved byissue 2879,accepted as a DR at the November, 2024 meeting.]

The specification ofdynamic_cast in7.6.1.7 [expr.dynamic.cast] paragraph 2 (andconst_castin 7.6.1.11 [expr.const.cast] is the same) says that the operandof a cast to an lvalue reference type must be an lvalue, so that

  struct A { virtual ~A(); }; A &&make_a();  A &&a = dynamic_cast<A&&>(make_a());   // ok  const A &b = dynamic_cast<const A&>(make_a()); // ill-formed

The behavior ofstatic_cast is an odd hybrid:

  struct B : A { }; B &&make_b();  A &&c = static_cast<A&&>(make_b()); // ok  const A &d = static_cast<const A&>(make_b()); // ok  const B &e = static_cast<const B&>(make_a()); // ill-formed

(Binding a const lvalue reference to an rvalue is permitted by7.6.1.9 [expr.static.cast] paragraph 4 but not by paragraphs 2 and3.)

There is implementation divergence on the treatment of these examples.

Also,const_cast permits binding an rvalue reference to aclass prvalue but not to any other kind of prvalue, which seems like anunnecessary restriction.

Finally, 7.6.1.9 [expr.static.cast] paragraph 3 allows binding anrvalue reference to a class or array prvalue, but not to other kinds ofprvalues; those are covered in paragraph 4. This would be less confusingif paragraph 3 only dealt with binding rvalue references to glvalues andleft all discussion of prvalues to paragraph 4, which adequately handlesthe class and array cases as well.

Notes from the May, 2015 meeting:

CWG reaffirmed the status quo fordynamic_cast but feltthatconst_cast should be changed to permit binding anrvalue reference to types that have associated memory (class andarray types).

CWG 2024-11-19

The last of the concerns is resolved by the resolution ofissue 2879.




[8]ページ先頭

©2009-2025 Movatter.jp