public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-110195-4-K2Lne2l8gq@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-110195-4@http.gcc.gnu.org/bugzilla/>

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(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.
> 
> 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() = 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
— no user-provided, explicit, or inherited constructors (15.1),
— no private or protected non-static data members (Clause 14),
— no virtual functions (13.3), and
— no virtual, private, or protected base classes (13.1).

The type above meets this definition. It has a user-declared constructor, but
not a user-provided constructor.

In C++20 the rule changed to:

An aggregate is an array or a class (Clause 11) with
 — no user-declared or inherited constructors (11.4.5),
 — no private or protected direct non-static data members (11.8),
 — no private or protected direct base classes (11.8.3), and
 — 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() = default;
};

Not an aggregate in any version of C++ because it has an explicit user-declared
constructor.

class Single
{
private:
    Single() {}
};

Not an aggregate in any version of C++ because it has a user-provided
constructor.

class Single
{
private:
    Single() = 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.

  parent reply	other threads:[~2023-06-09 20:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-09 15:21 [Bug c++/110195] New: " jack.cui2 at foxmail dot com
2023-06-09 15:27 ` [Bug c++/110195] " pinskia at gcc dot gnu.org
2023-06-09 15:35 ` jack.cui2 at foxmail dot com
2023-06-09 16:10 ` jack.cui2 at foxmail dot com
2023-06-09 16:13 ` pinskia at gcc dot gnu.org
2023-06-09 16:19 ` jack.cui2 at foxmail dot com
2023-06-09 20:52 ` redi at gcc dot gnu.org [this message]
2023-06-10  1:25 ` jack.cui2 at foxmail dot com
2023-06-10  8:18 ` redi at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-110195-4-K2Lne2l8gq@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).