https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104192

            Bug ID: 104192
           Summary: Uninitialized object read is not detected in a
                    constant expression
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: fchelnokov at gmail dot com
  Target Milestone: ---

In the following program, in a constant expression, a temporary object of A is
created with all fields initialized, and then function f creates another object
of A at the same address, skipping (re)initialization of the field x, which is
read afterwards:
```
#include <memory>

struct A {
    int x;
    constexpr A() {}
    constexpr A(int xx) : x(xx) {}
};

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

static_assert( f(A{5}) == 5 );
```
This code is accepted by GCC, but other compilers complain about uninitialized
object read. Demo: https://gcc.godbolt.org/z/sPGjj75Md

Related discussion: https://stackoverflow.com/q/70820127/7325599

Reply via email to