[class.base.init]

# 11 Classes [[class]](./#class)

## 11.9 Initialization [[class.init]](class.init#class.base.init)

### 11.9.3 Initializing bases and members [class.base.init]

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5485)

In the definition of a constructor for a class,
initializers for direct and virtual base class subobjects and
non-static data members can be specified by a[*ctor-initializer*](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]"),
which has the form

[ctor-initializer:](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]")  
: [*mem-initializer-list*](#nt:mem-initializer-list "11.9.3 Initializing bases and members [class.base.init]")

[mem-initializer-list:](#nt:mem-initializer-list "11.9.3 Initializing bases and members [class.base.init]")  
[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") ...opt  
[*mem-initializer-list*](#nt:mem-initializer-list "11.9.3 Initializing bases and members [class.base.init]") , [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") ...opt

[mem-initializer:](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]")  
[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") ( [*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]")opt )  
[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") [*braced-init-list*](dcl.init.general#nt:braced-init-list "9.5.1 General [dcl.init.general]")

[mem-initializer-id:](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]")  
[*class-or-decltype*](class.derived.general#nt:class-or-decltype "11.7.1 General [class.derived.general]")  
[*identifier*](lex.name#nt:identifier "5.11 Identifiers [lex.name]")

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5514)

Lookup for an unqualified name in a [*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") ignores the constructor's function parameter scope[.](#2.sentence-1)

[*Note [1](#note-1)*:

If the constructor's class contains a member with the same name as a direct
or virtual base class of the class, a[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") naming the member or base class and composed of a single identifier
refers to the class member[.](#2.sentence-2)

A[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") for the hidden base class can be specified using a qualified name[.](#2.sentence-3)

â *end note*]

Unless the[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") names the constructor's class,
a non-static data member of the constructor's class, or
a direct or virtual base of that class,
the[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is ill-formed[.](#2.sentence-4)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5536)

A[*mem-initializer-list*](#nt:mem-initializer-list "11.9.3 Initializing bases and members [class.base.init]") can initialize a base class using any [*class-or-decltype*](class.derived.general#nt:class-or-decltype "11.7.1 General [class.derived.general]") that denotes that base class type[.](#3.sentence-1)

[*Example [1](#example-1)*: struct A { A(); };typedef A global_A;struct B { };struct C: public A, public B { C(); };
C::C(): global_A() { } // mem-initializer for base A â *end example*]

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5550)

If a[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") is ambiguous because it designates both a direct non-virtual base class and
an indirect virtual base class, the[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is ill-formed[.](#4.sentence-1)

[*Example [2](#example-2)*: struct A { A(); };struct B: public virtual A { };struct C: public A, public B { C(); };
C::C(): A() { } // error: which A? â *end example*]

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5566)

A[*ctor-initializer*](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]") may initialize a variant member of the
constructor's class[.](#5.sentence-1)

If a[*ctor-initializer*](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]") specifies more than one[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") for the same member or for the same base class,
the[*ctor-initializer*](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]") is ill-formed[.](#5.sentence-2)

[6](#6)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5580)

A [*mem-initializer-list*](#nt:mem-initializer-list "11.9.3 Initializing bases and members [class.base.init]") can delegate to another
constructor of the constructor's class using any[*class-or-decltype*](class.derived.general#nt:class-or-decltype "11.7.1 General [class.derived.general]") that denotes the constructor's class itself[.](#6.sentence-1)

If a[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") designates the constructor's class,
it shall be the only [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]"); the constructor
is a [*delegating constructor*](#def:constructor,delegating "11.9.3 Initializing bases and members [class.base.init]"), and the constructor selected by the[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is the [*target constructor*](#def:constructor,target "11.9.3 Initializing bases and members [class.base.init]")[.](#6.sentence-2)

The target constructor is selected by overload resolution[.](#6.sentence-3)

Once the target constructor returns, the body of the delegating constructor
is executed[.](#6.sentence-4)

If a constructor delegates to itself directly or indirectly,
the program is ill-formed, no diagnostic required[.](#6.sentence-5)

[*Example [3](#example-3)*: struct C { C( int ) { } // #1: non-delegating constructor C(): C(42) { } // #2: delegates to #1 C( char c ) : C(42.0) { } // #3: ill-formed due to recursion with #4 C( double d ) : C('a') { } // #4: ill-formed due to recursion with #3}; â *end example*]

[7](#7)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5603)

The[*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]") or [*braced-init-list*](dcl.init.general#nt:braced-init-list "9.5.1 General [dcl.init.general]") in a[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is used to initialize the
designated subobject (or, in the case of a delegating constructor, the complete class object)
according to the initialization rules of [[dcl.init]](dcl.init "9.5 Initializers") for direct-initialization[.](#7.sentence-1)

[*Example [4](#example-4)*: struct B1 { B1(int); /* ... */ };struct B2 { B2(int); /* ... */ };struct D : B1, B2 { D(int);
 B1 b; const int c;};

D::D(int a) : B2(a+1), B1(a+2), c(a+3), b(a+4) { /* ... */ } D d(10); â *end example*]

[*Note [2](#note-2)*:

The initialization
performed by each [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") constitutes a full-expression ([[intro.execution]](intro.execution "6.10.1 Sequential execution"))[.](#7.sentence-2)

Any expression in
a[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is evaluated as part of the full-expression that performs the initialization[.](#7.sentence-3)

â *end note*]

A [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") where the [*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") denotes
a virtual base class is ignored during execution of a constructor of any class that is
not the most derived class[.](#7.sentence-4)

[8](#8)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5641)

A temporary expression bound to a reference member in a [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is ill-formed[.](#8.sentence-1)

[*Example [5](#example-5)*: struct A { A() : v(42) { } // errorconst int& v;}; â *end example*]

[9](#9)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5653)

In a non-delegating constructor
other than an implicitly-defined copy/move constructor ([[class.copy.ctor]](class.copy.ctor "11.4.5.3 Copy/move constructors")),
if a given potentially constructed subobject is not designated by a[*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") (including the case where there is no[*mem-initializer-list*](#nt:mem-initializer-list "11.9.3 Initializing bases and members [class.base.init]") because the constructor has no[*ctor-initializer*](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]")),
then

- [(9.1)](#9.1)

if the entity is a non-static data member that has
a default member initializer ([[class.mem]](class.mem "11.4 Class members")) and either
  * [(9.1.1)](#9.1.1)

the constructor's class is a union ([[class.union]](class.union "11.5 Unions")), and no other variant
member of that union is designated by a [*mem-initializer-id*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]") or

  * [(9.1.2)](#9.1.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*](#nt:mem-initializer-id "11.9.3 Initializing bases and members [class.base.init]"),

the entity is initialized from its default member initializer
as specified in [[dcl.init]](dcl.init "9.5 Initializers");

- [(9.2)](#9.2)

otherwise, if the entity is an anonymous union or a variant member ([[class.union.anon]](class.union.anon "11.5.2 Anonymous unions")), no initialization is performed;

- [(9.3)](#9.3)

otherwise, the entity is default-initialized ([[dcl.init]](dcl.init "9.5 Initializers"))[.](#9.sentence-1)

[*Note [3](#note-3)*:

An abstract class ([[class.abstract]](class.abstract "11.7.4 Abstract classes")) is never a most derived
class, thus its constructors never initialize virtual base classes, therefore the
corresponding [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]")*s* can be omitted[.](#9.sentence-2)

â *end note*]

An attempt to initialize more than one non-static data member of a union renders the
program ill-formed[.](#9.sentence-3)

[*Note [4](#note-4)*:

After the call to a constructor for classX for an object with automatic or dynamic storage duration
has completed, if
the constructor was not invoked as part of value-initialization and
a member ofX is neither initialized nor
given a value
during execution of the [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") of the body of the constructor,
the member has an indeterminate or erroneous value ([[basic.indet]](basic.indet "6.8.5 Indeterminate and erroneous values"))[.](#9.sentence-4)

â *end note*]

[*Example [6](#example-6)*: struct A { A();};

struct B { B(int);};

struct C { C() { } // initializes members as follows: A a; // OK, calls A​::​A()const B b; // error: B has no default constructorint i; // OK, i has indeterminate or erroneous valueint j = 5; // OK, j has the value 5}; â *end example*]

[10](#10)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5724)

If a given non-static data member has both a default member initializer
and a [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]"), the initialization specified by the[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is performed, and the non-static data member's
default member initializer is ignored[.](#10.sentence-1)

[*Example [7](#example-7)*:

Givenstruct A {int i = /* some integer expression with side effects */ ;
 A(int arg) : i(arg) { }// ...}; the A(int) constructor will simply initialize i to the value ofarg, and theside effects in i's default member initializer
will not take place[.](#10.sentence-2)

â *end example*]

[11](#11)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5748)

A temporary expression bound to a reference member from a
default member initializer is ill-formed[.](#11.sentence-1)

[*Example [8](#example-8)*: struct A { A() = default; // OK A(int v) : v(v) { } // OKconst int& v = 42; // OK};
A a1; // error: ill-formed binding of temporary to reference A a2(1); // OK, unfortunately â *end example*]

[12](#12)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5763)

In a non-delegating constructor, the destructor for each potentially constructed
subobject of class type is potentially invoked ([[class.dtor]](class.dtor "11.4.7 Destructors"))[.](#12.sentence-1)

[*Note [5](#note-5)*:

This provision ensures that destructors can be called for fully-constructed
subobjects in case an exception is thrown ([[except.ctor]](except.ctor "14.3 Stack unwinding"))[.](#12.sentence-2)

â *end note*]

[13](#13)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5771)

In a non-delegating constructor, initialization
proceeds in the following order:

- [(13.1)](#13.1)

  First, and only for the constructor of the most derived class ([[intro.object]](intro.object "6.8.2 Object model")),
virtual base classes are initialized in the order they appear on a
depth-first left-to-right traversal of the directed acyclic graph of
base classes,
where âleft-to-rightâ is the order of appearance of the base classes
in the derived class[*base-specifier-list*](class.derived.general#nt:base-specifier-list "11.7.1 General [class.derived.general]")[.](#13.1.sentence-1)

- [(13.2)](#13.2)

  Then, direct base classes are initialized in declaration order
as they appear in the[*base-specifier-list*](class.derived.general#nt:base-specifier-list "11.7.1 General [class.derived.general]") (regardless of the order of the[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]")*s*)[.](#13.2.sentence-1)

- [(13.3)](#13.3)

  Then, non-static data members are initialized in the order
they were declared in the class definition
(again regardless of the order of the[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]")*s*)[.](#13.3.sentence-1)

- [(13.4)](#13.4)

  Finally, the [*compound-statement*](stmt.block#nt:compound-statement "8.4 Compound statement or block [stmt.block]") of the constructor
body is executed[.](#13.4.sentence-1)

[*Note [6](#note-6)*:

The declaration order is mandated to ensure that base and member
subobjects are destroyed in the reverse order of initialization[.](#13.sentence-2)

â *end note*]

[14](#14)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5807)

[*Example [9](#example-9)*: struct V { V();
 V(int);};

struct A : virtual V { A();
 A(int);};

struct B : virtual V { B();
 B(int);};

struct C : A, B, virtual V { C();
 C(int);};

A::A(int i) : V(i) { /* ... */ } B::B(int i) { /* ... */ } C::C(int i) { /* ... */ } V v(1); // use V(int) A a(2); // use V(int) B b(3); // use V() C c(4); // use V() â *end example*]

[15](#15)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5841)

[*Note [7](#note-7)*:

The [*expression-list*](expr.post.general#nt:expression-list "7.6.1.1 General [expr.post.general]") or [*braced-init-list*](dcl.init.general#nt:braced-init-list "9.5.1 General [dcl.init.general]") of a [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") is in the function parameter scope of the constructor
and can use this to refer to the object being initialized[.](#15.sentence-1)

â *end note*]

[*Example [10](#example-10)*: class X {int a; int b; int i; int j;public:const int& r;
 X(int i): r(a), b(i), i(i), j(this->i) { }};

initializesX​::​r to refer toX​::​a,
initializesX​::​b with the value of the constructor parameteri,
initializesX​::​i with the value of the constructor parameteri,
and initializesX​::​j with the value ofX​::​i;
this takes place each time an object of classX is created[.](#15.sentence-2)

â *end example*]

[16](#16)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5883)

Member functions (including virtual member functions, [[class.virtual]](class.virtual "11.7.3 Virtual functions")) can be
called for an object under construction or destruction[.](#16.sentence-1)

Similarly, an object under construction or destruction can be the operand of thetypeid operator ([[expr.typeid]](expr.typeid "7.6.1.8 Type identification")) or of adynamic_cast ([[expr.dynamic.cast]](expr.dynamic.cast "7.6.1.7 Dynamic cast"))[.](#16.sentence-2)

However, if these operations are performed
during evaluation of

- [(16.1)](#16.1)

a [*ctor-initializer*](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]") (or in a function called directly or indirectly from a[*ctor-initializer*](#nt:ctor-initializer "11.9.3 Initializing bases and members [class.base.init]"))
before all the[*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]")*s* for base classes have completed,

- [(16.2)](#16.2)

a precondition assertion of a constructor, or

- [(16.3)](#16.3)

a postcondition assertion of a destructor ([[dcl.contract.func]](dcl.contract.func "9.4.1 General")),

the program has undefined behavior[.](#16.sentence-3)

[*Example [11](#example-11)*: class A {public: A(int);};

class B : public A {int j;public:int f();
 B() : A(f()), // undefined behavior: calls member function but base A not yet initialized j(f()) { } // well-defined: bases are all initialized};

class C {public: C(int);};

class D : public B, C {int i;public: D() : C(f()), // undefined behavior: calls member function but base C not yet initialized i(f()) { } // well-defined: bases are all initialized}; â *end example*]

[17](#17)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5936)

[*Note [8](#note-8)*:

[[class.cdtor]](class.cdtor "11.9.5 Construction and destruction") describes the results of virtual function calls,typeid anddynamic_casts
during construction for the well-defined cases;
that is, describes the polymorphic behavior
of an object under construction[.](#17.sentence-1)

â *end note*]

[18](#18)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/classes.tex#L5947)

A [*mem-initializer*](#nt:mem-initializer "11.9.3 Initializing bases and members [class.base.init]") followed by an ellipsis is
a pack expansion ([[temp.variadic]](temp.variadic "13.7.4 Variadic templates")) that initializes the base
classes specified by a pack expansion in the [*base-specifier-list*](class.derived.general#nt:base-specifier-list "11.7.1 General [class.derived.general]") for the class[.](#18.sentence-1)

[*Example [12](#example-12)*: template<class... Mixins>class X : public Mixins... {public: X(const Mixins&... mixins) : Mixins(mixins)... { }}; â *end example*]
