From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 1014F3858C83; Wed, 20 Jul 2022 21:31:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1014F3858C83 From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/106150] [DR 2084] union with more than one variant and non-trivial constructor is not accepted Date: Wed, 20 Jul 2022 21:31:56 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 12.1.0 X-Bugzilla-Keywords: rejects-valid X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: see_also Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jul 2022 21:31:57 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D106150 Jonathan Wakely changed: What |Removed |Added ---------------------------------------------------------------------------- See Also| |https://gcc.gnu.org/bugzill | |a/show_bug.cgi?id=3D98423 --- Comment #7 from Jonathan Wakely --- (In reply to Andrew Pinski from comment #3) > Here is an example which is valid (after > https://cplusplus.github.io/CWG/issues/2084.html): > struct S1 { > S1(); > }; > struct S { > S(); > }; > union U { > S s{}; > S1 s1; > } u; >=20 > The check in GCC for this seems to be off, if only the variant s is there, > GCC (and clang) accepts it. >=20 > So the full check for the defect report was never really done (and it was > not even mentioned in the defect report commentary either). Yes, all of gcc, clang, edg and msvc reject cases like this. It should not matter that S1 does not have a trivial default ctor, because = the default member initializer should make this equivalent to: union U { S s; S1 s1; U() : s() { } }; Having to write a user-provided constructor is annoying, because to do it "right" in a generic std::lib template requires: constexpr U() noexcept(is_nothrow_default_constructible_v) requires default_initializable { } But that should be approximately how the defaulted default ctor is defined automatically, without all that verbosity. This is a dup of PR 98423, where Jakub pointed out the code that needs to change, and what needs to happen.=