From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 19EE93858D3C; Fri, 9 Jun 2023 20:52:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 19EE93858D3C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1686343964; bh=tMysOmVXMCbhise1dAx3GQOIuvmxlgbQ0h3lWGfMmdI=; h=From:To:Subject:Date:In-Reply-To:References:From; b=ApIqx8fvG+eh2hKsBB3SPbC9jryMIigFEI1utjCNrKoeb/FxEhvYY3cr7+q+ITMiy B+BQ/hdyMLHY8m5iA6xVPGobTuFlCezTWD0LuDZt6I3P8nDYVqRTUmD8E4eaMIszhx CiwUIVmNwqO5HOJmyjxCBAki6Fj8ht+CeSpC1G3I= From: "redi at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/110195] defaulted constructor does not respect the private accessor Date: Fri, 09 Jun 2023 20:52:43 +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: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: redi at gcc dot gnu.org X-Bugzilla-Status: RESOLVED X-Bugzilla-Resolution: INVALID 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: resolution bug_status 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 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D110195 Jonathan Wakely changed: What |Removed |Added ---------------------------------------------------------------------------- Resolution|--- |INVALID Status|UNCONFIRMED |RESOLVED --- Comment #6 from Jonathan Wakely --- (In reply to jack from comment #2) > (In reply to Andrew Pinski from comment #1) > > Before C++20, Single{} didn't call the constructor so this behavior is > > expected. >=20 > Could you explain why it didn't call the constructor before c++20? C++ > standard rules or compiler make it this way? The standard. class Single { private: Single() =3D default; }; In C++17 Single is an aggregate, and Single{} is aggregate-initialization, which initializes each member in turn, without calling a constructor. Since= it doesn't use the constructor, it doesn't matter if it's private. In C++20 it's not an aggregate, so Single{} does value-initialization using= the constructor, which is private. The C++17 rule is: An aggregate is an array or a class (Clause 12) with =E2=80=94 no user-provided, explicit, or inherited constructors (15.1), =E2=80=94 no private or protected non-static data members (Clause 14), =E2=80=94 no virtual functions (13.3), and =E2=80=94 no virtual, private, or protected base classes (13.1). The type above meets this definition. It has a user-declared constructor, b= ut not a user-provided constructor. In C++20 the rule changed to: An aggregate is an array or a class (Clause 11) with =E2=80=94 no user-declared or inherited constructors (11.4.5), =E2=80=94 no private or protected direct non-static data members (11.8), =E2=80=94 no private or protected direct base classes (11.8.3), and =E2=80=94 no virtual functions (11.7.3) or virtual base classes (11.7.2). Now the user-declared constructor means it's not an aggregate. For the other classes: class Single { private: explicit Single() =3D default; }; Not an aggregate in any version of C++ because it has an explicit user-decl= ared constructor. class Single { private: Single() {} }; Not an aggregate in any version of C++ because it has a user-provided constructor. class Single { private: Single() =3D default; int a; }; Not an aggregate in any version of C++ because it has a private member. In all these cases the type is not an aggregate, so Single{} always does value-initialization and always uses the constructor. GCC is doing exactly what the standard requires.=