Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialisation of aggregate with default constructor deleted in c++20

There is a struct containing POD and default constructor deleted. Trying to aggregate-initalize an instance of the struct results in compilation error in g++9.1 when compiled with -std=c++2a. The same code compiles fine with -std=c++17.

https://godbolt.org/z/xlRHLL

struct S
{
    int a;
    S() = delete;
};

int main()
{
    S s {.a = 0};
}
like image 561
Alexey R. Avatar asked Jul 22 '19 21:07

Alexey R.


1 Answers

Your struct is not an aggregate since C++20.

The definition of aggregate was changed once again:

cppreference

An aggregate is one of the following types:

  • ...

  • class type (typically, struct or union), that has

    • ...

    • no user-provided, inherited, or explicit constructors (explicitly defaulted or deleted constructors are allowed)
      (since C++17) (until C++20)

    • no user-declared or inherited constructors
      (since C++20)

IMO, this fixes a defect in the language. Being able to construct (with aggregate initializaton) objects with deleted or inaccessible constructors doesn't look right to me.

like image 172
HolyBlackCat Avatar answered Oct 07 '22 05:10

HolyBlackCat