Aggregate initialization does not support constructor access

Given the example below, I was surprised to find that even though the default constructor was explicitly removable (or made by default in this regard), aggregate initialization remained possible.

#include <iostream>

struct DefaultPrivate
{
      const int n_;
      static const DefaultPrivate& create();

    private:
      DefaultPrivate() = delete;
};

const DefaultPrivate& DefaultPrivate::create()
{
    static DefaultPrivate result{10};
    return result;
}

int main() {
    DefaultPrivate x; //Fails
    DefaultPrivate y{10};//Works
    return 0;
}

Is the relationship between the private default construct (or remote) and aggregate initialization not specified in the standard?

This was on both GCC 6.3 and VCC 2017

The reason I ask the question was because I was hoping that changing access to the default constructor would prevent public aggregate initialization

+6
source share
1

++ 11, ,

T , .

++ 11 aggregate :

...

(, struct union),

  • ...

  • , inherited, or explicit (since C++17) (explicitly defaulted or deleted constructors are allowed) (since C++11)

  • ...

, ++ 11 - , .

:

direct public base, (since C++17) / .

, DefaultPrivate y{10}; , , delete private, .

BTW: DefaultPrivate x;

T - non-POD (until C++11), . ( ) ;

, delete ed, .

, DefaultPrivate x{};, ; n_ ( zero initialized) 0.

LIVE

+4

All Articles