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

            Bug ID: 102286
           Summary: [constexpr] construct_at incorrectly starts union
                    array lifetime in some cases
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ldalessandro at gmail dot com
  Target Milestone: ---

Given the following struct and union,

    struct Foo {
        constexpr explicit Foo(int) {}
    };

    union U {
        Foo data[1];
        constexpr U() {} // no active member
    };

In the function foo() below, assignment to u.data[0] correctly starts the
lifetime of the data array by https://eel.is/c++draft/class.union#general-6.

    constexpr void foo() {
        U u;
        u.data[0] = 0; // okay
    }

However in the function bar() below, &u.data[0] forms a reference before the
start of the lifetime of the data array and thus std::construct_at cannot be
used to start the lifetime of data.

    constexpr void bar() {
        U u;
        std::construct_at(&u.data[0], 0); // forms reference outside lifetime
    }

Currently gcc and msvc accept this invalid code, while clang and icc reject.

See the thread of discussion at
https://lists.isocpp.org/std-proposals/2021/09/3171.php and a live example at
https://godbolt.org/z/Poh6bb5de.

Reply via email to