Constant evaluation of self-assignment in member initialization

Viewed 49

In the following program, constexpr function foo() makes an object of A with the field x=1, then constructs another object on top of it using std::construct_at and default initialization x=x, then the constant evaluated value is printed:

#include <memory>
#include <iostream>

struct A {
    int x = x;
};

constexpr int foo() {
    A a{1};
    std::construct_at<A>(&a);
    return a.x;
}

constexpr int v = foo();

int main() {
    std::cout << v;
}

GCC prints 1 here. Both Clang and MSVC print 0. And only Clang issues a warning: field 'x' is uninitialized when used. Demo: https://gcc.godbolt.org/z/WTsxdrj8e

Is there an undefined behavior in the program? If yes, why does no compiler detect it during constant evaluation? If no, which compiler is right?

0 Answers
Related