Title
C-style casts to reference types
Status
open
Section
7.6.3 [expr.cast]
Submitter
Hubert Tong

Created on 2024-03-20.00:00:00 last changed 1 month ago

Messages

Date: 2024-03-20.00:00:00

(From submission #522.)

Consider:

  struct A {};
  struct B : A {};
  const B b;
  void f() {
    (A &&)b;          // #1
    const_cast<A &&>(static_cast<const A &>(b));
    const_cast<A &&>(static_cast<const volatile A &&>(b));
    const_cast<A &&>(static_cast<A>(b));  // slicing interpretation
  }

Is #1 ill-formed because of the three alternative valid interpretations shown in the following lines?

Also consider:

  struct B { };
  const B f();
  B& b1 = const_cast<B&>(static_cast<const B&>(f())); // OK
  B& b2 = (B&)f(); // ???

There is implementation divergence: gcc and MSVC accept, clang and EDG accept b1 and reject b2. If f is changed to return non-const, gcc also rejects b2.

For another example:

  struct A {
    operator const B() = delete;
  } a;

  B& b3 = const_cast<B&>(static_cast<const B&>(a)); // error, deleted
  B& b4 = (B&)a; // error or reinterpret_cast?

Implementations agree that b3 is ill-formed for selecting the deleted conversion operator function, but b4 is considered a reinterpret_cast by the majority of implementations.

History
Date User Action Args
2024-03-20 00:00:00admincreate