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

            Bug ID: 98637
           Summary: Changing active union member via assignment expression
                    should require trivial default constructor in
                    constexpr context
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: m.cencora at gmail dot com
  Target Milestone: ---

According to http://eel.is/c++draft/class.union#general-6 following code should
be rejected
because changing of active union member via assignment expression should be
allowed only for classes with non-deleted trivial default constructor.

This is per Richard Smith's comment in
https://bugs.llvm.org/show_bug.cgi?id=48453

g++ -std=c++20

#include <memory>

struct NonTrivial
{
    constexpr NonTrivial() : b(true) {}

    bool b;
};

union Un
{
    bool f1;
    NonTrivial f2;
};

constexpr bool test()
{
    Un un{ .f1 = false };
    un.f2 = {}; // should be an error here, and require
std::construct_at(&un.f2);
    return un.f2.b;
}

static_assert(test());

Reply via email to