This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 118b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2025-09-28


3046. Enumerations as part of the common initial sequence

Section: 9.8.1  [dcl.enum]     Status: open     Submitter: Benjamin Sch.     Date: 2025-07-28

(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.