3

I am trying to understand the exact behavior of value initialization by T() or T{} for a class type T in C++11. What confuses me are these two snippets taken from http://en.cppreference.com:

Value Initialization:

The effects of value initialization are: [...]

1) if T is a class type with no default constructor or with a user-provided or deleted default constructor, the object is default-initialized; (since C++11)

[...]

so I looked up Default-Initialization:

The effects of default initialization are:

  • if T is a [...] class type, the constructors are considered and subjected to overload resolution against the empty argument list. The constructor selected (which is one of the default constructors) is called to provide the initial value for the new object;

[...]

So this basically says that if T is a class type and its implicit default constructor is not available, then the object will be constructed by a call to one of its default constructors? In my understanding, this makes only sense for the mentioned case of a user-provided default constructor; then, upon construction, only what is explicitly stated in that constructor will be executed, and every member not explicitly initialized will get default-initialized (please correct me if I am wrong here).

My questions:

1) What would happen if there was no user-provided default constructor and there was no default constructor or it was deleted? I would guess the code would not compile. If this is right, then:

2) What is the need to also explicitly mention the cases "no default constructor" and "deleted default constructor"?

1
  • should it be "defaulted default constructor" ? Commented May 13, 2018 at 16:44

1 Answer 1

2

The wording on cppreference doesn't seem to match that in the standard. C++11 8.5/7 [dcl.init]:

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized.

An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to “constructed” objects, objects “for which the constructor has completed,” etc., even if no constructor is invoked for the object’s initialization.

For comparison, this is the wording in C++14 (n4140) 8.5/7 [dcl.init]:

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a default constructor that is user-provided or deleted, then the object is default-initialized;
  • if T is a (possibly cv-qualified) class type without a user-provided or deleted default constructor, then the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized.

An object that is value-initialized is deemed to be constructed and thus subject to provisions of this International Standard applying to “constructed” objects, objects “for which the constructor has completed,” etc., even if no constructor is invoked for the object’s initialization.

Sign up to request clarification or add additional context in comments.

2 Comments

Does that value initialisation happens when there is no default constructor..?? Isn’t it compiler error.?
@setia_ The first bullet point (the one with user-provided constructors) says: "and the initialization is ill-formed if T has no accessible default constructor". Or did you mean the C++14 version? That one declares it ill-formed "indirectly" by delegating to default-initialisation (which is ill-formed when there's no usable default ctor).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.