Title
Ambiguous interpretation of C-style cast
Status
dr
Section
7.6.3 [expr.cast]
Submitter
Jim X

Created on 2022-03-21.00:00:00 last changed 3 weeks ago

Messages

Date: 2024-03-20.07:17:09

Proposed resolution (approved by CWG 2024-03-20):

Change in 7.6.3 [expr.cast] paragraph 4 as follows:

... If a conversion can be interpreted in more than one of the ways listed above, the interpretation that appears first in the list is used, even if a cast resulting from that interpretation is ill-formed. If a conversion can be interpreted in more than one way as a static_cast followed by a const_cast is used and the conversion can be interpreted in more than one way as such, the conversion is ill-formed. [ Example 1 :
  struct A { };
  struct I1 : A { };
  struct I2 : A { };
  struct D : I1, I2 { };
  A* foo( D* p ) {
    return (A*)( p );  // ill-formed static_cast interpretation
  }

  int*** ptr = 0;
  auto t = (int const*const*const*)ptr;  // OK, const_cast interpretation

  struct S {
    operator const int*();
    operator volatile int*();
  };
  int *p = (int*)S();  // error: two possible interpretations using static_cast followed by const_cast
-- end example ]
Date: 2024-03-15.00:00:00

[Accepted as a DR at the March, 2024 meeting.]

(From editorial issue 5355.)

Consider:

   int*** ptr = 0;
   auto t = (int const*const*const*)ptr;

There is more than one way how this can be interpreted as a static_cast followed by a const_cast, namely:

  const_cast<int const * const * const * >(static_cast<int * * const * >(ptr));
  const_cast<int const * const * const * >(static_cast<int * const * const * >(ptr));

Subclause 7.6.3 [expr.cast] paragraph 4 makes such a program ill-formed:

... 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.
History
Date User Action Args
2024-04-05 21:43:46adminsetstatus: ready -> dr
2024-03-20 07:17:09adminsetstatus: tentatively ready -> ready
2024-01-21 13:30:12adminsetstatus: review -> tentatively ready
2023-12-16 00:18:01adminsetmessages: + msg7558
2022-03-21 00:00:00admincreate