C++11 default constructor behavior with class member default value

Tyson Hilmer

This question is for C++11.

In the following struct A, will x always be 42, when the default constructor is used?

struct A{
  A() = default;
private:
  int x = 42;
}

In short, I'm wondering if the default constructor guarantees that default member values will be set.

Lightness Races in Orbit

Yes.

Unfortunately the wording below is from the standard draft as of today, but the principle is the same in C++11.

[class.default.ctor]/4 A default constructor that is defaulted and not defined as deleted is implicitly defined when it is odr-used ([basic.def.odr]) to create an object of its class type ([intro.object]), when it is needed for constant evaluation ([expr.const]), or when it is explicitly defaulted after its first declaration. The implicitly-defined default constructor performs the set of initializations of the class that would be performed by a user-written default constructor for that class with no ctor-initializer ([class.base.init]) and an empty compound-statement.

[class.base.init]/9 In a non-delegating constructor, if a given potentially constructed subobject is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer), then:

  1. if the entity is a non-static data member that has a default member initializer ([class.mem]) and either
    1. the constructor's class is a union ([class.union]), and no other variant member of that union is designated by a mem-initializer-id or
    2. the constructor's class is not a union, and, if the entity is a member of an anonymous union, no other member of that union is designated by a mem-initializer-id, the entity is initialized from its default member initializer as specified in [dcl.init];

[..]


In short, I'm wondering if the default constructor guarantees that default member values will be set.

An example of exactly this follows the passage latterly quoted above.


However, if you were to define A::A() and provide an initialiser for x, it would take precedence over the inline initialiser.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

C++11: Use non-static member function as default argument in class constructor

default value for a class member in c++

C++11 default class member initialization with initializer list , simultaneously

default value for struct member in C

call the default constructor for the pointer of class as a value of map in c++

Giving a char array a default value with a constructor in a class c++

Retrieve default value of in-class initialized member

C++11 private default constructor

C++11: Default constructor: Implicit or Explicit?

Default value for options object in class constructor

Default value for optional field in a constructor of a generic class

Why cant we delay initialise a class member with a non default constructor?

How to initialize a class member reference to the empty string in the default constructor

Disable default class member initialization before constructor code

What is the behavior of a defaulted default constructor with in-class initialization?

Default member values or default constructor parameters in structures?

Default value of a protected member c++

C++: No default constructor exists for parent class

C++ base class constructor with default parameters

No default constructor exists for class c++

C++ class object default constructor bug

Default move constructor with mutex member

Using a class data-member in a class function as default value, c++

C++11: Can defaulting default constructor lead to partially initialized class?

C++ 11 - If I initialize a variable in a class header file, is a default constructor generated?

Default Constructor in class file

No default constructor exists for a class

Is it possible to override the default behavior of getting a struct member in C++?

Cpp/C++ Assign Default value to default constructor