Title
Enumerations as part of the common initial sequence
Status
open
Section
9.8.1 [dcl.enum]
Submitter
Benjamin Sch.

Created on 2025-07-28.00:00:00 last changed 1 month ago

Messages

Date: 2025-07-28.00:00:00

(From submission #732.)

Consider:

  #include <iostream>
  #include <type_traits>

  enum E { E0 } e;
  enum F { F0, F1, F2, F3 };
  static_assert(std::is_same_v<std::underlying_type_t<E>, std::underlying_type_t<F>>);  // assume this passes

  struct A { E e; };
  struct B { F f; };
  union U {
    A a;
    B b;
  } u;

  bool test() {
    return u.a.e == 2;
  }

  auto ptest = test;

  int main() {
    u.b.f = F2;
    std::cout << ptest();
  }

Both u.a and u.b are part of the common initial sequence, allowing u.a.e to read the value of u.a.f, even though E cannot represent all values of F.

Possible resolution:

Change in 6.9.1 [basic.types.general] paragraph 10 as follows:

Two enumeration types are layout-compatible enumerations if they have the same underlying type values.
History
Date User Action Args
2025-07-28 00:00:00admincreate