vendredi 27 octobre 2017

Aggregate initialization does not uphold constructor access

Given the example here below, I was surprised to find that despite the default constructor explicitly being deleted, 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;
}

The latest VC++ compiler, distributed with Studio 2017, even allowed aggregate initialization despite adding constructor below (contrary to GCC 6.3).

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

    private:
      DefaultPrivate() = delete;
      DefaultPrivate(int n): n_(n){} //Causes failure on GCC, but not VCC
};

Is the relation between private default construction and aggregate initialization unspecified in the standard?

Aucun commentaire:

Enregistrer un commentaire