7
votes

From what cppref says about value initialization

if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized;

But since that class type has deleted default constructor, how could the object be default-initialized?

As far as I know, default initialization of class type needs the access of default constructor. If we have:

struct A {
    A() = delete;
    int k;
};

Then A *a = new A; will fail, so does A* a = new A();.

But A a{}; is OK. But why? According to cppreference

Otherwise, If the braced-init-list is empty and T is a class type with a default constructor, value-initialization is performed.

2
Working example: godbolt.org/g/f8CWAFRichard Critten
@RichardCritten : That's on the right path, but that's not value-initialization, it's list-initialization. :-]ildjarn
@RichardCritten See my newly edited post.This still requires a accessible default constructor. So I do not know why A a{} could work here.scottxiao
@ildjarn, you're right, sorry about that; change foo f{10}; to foo f{};.Richard Critten

2 Answers

7
votes

I think the standard just means "if T is a class type with deleted default constructor then goto default initialization". It would fail at last because the constructor selected for default initialization is deleted. It's used to distinguish with the 2nd case, i.e. "if T is a class type with a default constructor that is neither user-provided nor deleted", for that case zero-initialization is performed firstly, then default-initialization if T has a non-trivial default constructor.

A a{} is OK,but why?

Because when A is an aggregate type aggregate initialization is performed. Note that explicitly deleted constructors are allowed for aggregate type since C++11.

In all cases, if the empty pair of braces {} is used and T is an aggregate type, aggregate-initialization is performed instead of value-initialization.

And

An aggregate is one of the following types:

  • array type
  • class type (typically, struct or union), that has
    • no private or protected non-static data members
    • no user-provided, inherited, or explicit (since C++17) constructors (explicitly defaulted or deleted constructors are allowed) (since C++11)
5
votes

But since that class type has deleted default constructor, how could the object be default-initialized?

Stating that the object is default-initialized doesn't in of itself imply that default-initialization is a valid thing to do. The wording just forwards us into that process, it makes the initialization process coherent, it doesn't imply that it must succeed.

For instance:

struct A {
    A() = delete;
    A(int); // not an aggregate
};

auto a = A(); 

The initializer is (), so this the object is value-initialized. In this case, that means it's default-initialized, which means we find a constructor.

Calling that constructor happens to be ill-formed, because it's deleted, which makes the whole initialization ill-formed. But we get to that point by way of default-initialization.