Skip to content

incorrect compiler error "expression is not an integral constant expression" in enum declaration #113092

@djnz00

Description

@djnz00

clang 18 incorrectly reports an error declaring an enum as the result of a compile-time evaluated integer expression.

reduced test case below. clang version 18.1.8 Target: x86_64-pc-linux-gnu

#include <iostream>
#include <memory>

struct B { int i; };

struct A {
  constexpr A(int i) { std::construct_at(__builtin_addressof(b), i); }
  constexpr ~A() { std::destroy_at(__builtin_addressof(b)); }
  union { B b; };
};

consteval int i() { return A{42}.b.i; } // wrapper to circumvent compiler bug

int main() {
  // enum { I = i() };		// no error with both clang and gcc
  enum { I = A{42}.b.i };	// error with clang, but not with gcc
  std::cout << I << '\n';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:frontendLanguage frontend issues, e.g. anything involving "Sema"constexprAnything related to constant evaluation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions