Skip to content

CWG2878 [expr.cast] C-style casts to rvalue reference to base class type and casting away const unintentionally ill-formed #522

Description

@hubert-reinterpretcast

Full name of submitter (unless configured in github; will be published with the issue): Hubert Tong

Reference (section label): expr.cast

Link to reflector thread (if any): N/A

Issue description:
In https://eel.is/c++draft/expr.cast#4, we say:

If a conversion can be interpreted in more than one way as a static_cast followed by a const_cast, the conversion is ill-formed.

In the following, we see that the choice of static_cast can differ between casting to an lvalue reference type or an rvalue reference type.

struct A {};
struct B : A {};
const B b;
void f() {
  (A &&)b; // ill-formed?
  const_cast<A &&>(static_cast<const A &>(b));
  const_cast<A &&>(static_cast<const volatile A &&>(b));
}

Suggested resolution:
If a conversion can be interpreted in more than one way as a static_cast, not arbitrarily adding cv-qualification and to an rvalue reference type if and only if converting to an rvalue reference type, followed by a const_cast, the conversion is ill-formed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions