static_cast from "pointer to void" does not handle similar types
Section [expr.static.cast]
Richard Smith

Created on 2022-06-28.00:00:00 last changed 1 month ago


Date: 2022-07-01.20:56:37

Proposed resolution (approved by CWG 2022-07-01):

Change in [expr.static.cast] paragraph 13 as follows:

... Otherwise, if the original pointer value points to an object a, and there is an object b of type similar to T (ignoring cv-qualification) that is pointer-interconvertible (6.8.3 [basic.compound]) with a, the result is a pointer to b. Otherwise, the pointer value is unchanged by the conversion.
Date: 2022-06-28.00:00:00


  struct S {
    int a[5];
  } s;
  int (*p)[] = reinterpret_cast<int(*)[]>(&s);
  int n = (*p)[0];

This ought to have defined behavior: a pointer to s and a pointer to s.a are pointer-interconvertible, so you should be able to navigate between them this way. But the cast as shown does not work, because the type of the pointer-interconvertible object is int[5], not int[].

Date User Action Args
2022-07-01 20:56:37adminsetstatus: open -> ready
2022-06-30 16:25:58adminsetmessages: + msg6874
2022-06-28 00:00:00admincreate