[dcl.init] (original) (raw)

9 Declarations [dcl.dcl]

9.4 Initializers [dcl.init]

9.4.1 General [dcl.init.general]

The process of initialization described in [dcl.init] applies to all initializations regardless of syntactic context, including the initialization of a function parameter ([expr.call]), the initialization of a return value ([stmt.return]), or when an initializer follows a declarator.

[Note 1:

The rules in [dcl.init] apply even if the grammar permits only the brace-or-equal-initializer form of initializer in a given context.

— _end note_]

Except for objects declared with the constexpr specifier, for which see [dcl.constexpr], an initializer in the definition of a variable can consist of arbitrary expressions involving literals and previously declared variables and functions, regardless of the variable's storage duration.

[Example 1: int f(int);int a = 2;int b = f(a);int c(b); — _end example_]

[Note 3:

The order of initialization of variables with static storage duration is described in [basic.start]and [stmt.dcl].

— _end note_]

A declaration of a block-scope variable with external or internal linkage that has an initializer is ill-formed.

Tozero-initializean object or reference of typeTmeans:

Todefault-initializean object of typeTmeans:

A class type T is const-default-constructible if default-initialization of T would invoke a user-provided constructor of T (not inherited from a base class) or if

If a program calls for the default-initialization of an object of a const-qualified type T,T shall be a const-default-constructible class type or array thereof.

Tovalue-initializean object of typeTmeans:

A program that calls for default-initialization or value-initialization of an entity of reference type is ill-formed.

[Note 4:

For every object of static storage duration, static initialization ([basic.start.static]) is performed at program startup before any other initialization takes place.

In some cases, additional initialization is done later.

— _end note_]

If no initializer is specified for an object, the object is default-initialized.

An initializer for a static member is in the scope of the member's class.

[Example 2: int a;struct X { static int a;static int b;};int X::a = 1;int X::b = a; — _end example_]

If the entity being initialized does not have class type, theexpression-list in a parenthesized initializer shall be a single expression.

The semantics of initializers are as follows.

Thedestination typeis the type of the object or reference being initialized and thesource typeis the type of the initializer expression.

If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.

If the initializer is a parenthesized expression-list, the expressions are evaluated in the order specified for function calls ([expr.call]).

An object whose initialization has completed is deemed to be constructed, even if the object is of non-class type or no constructor of the object's class is invoked for the initialization.

[Note 9:

Such an object might have been value-initialized or initialized by aggregate initialization ([dcl.init.aggr]) or by an inherited constructor ([class.inhctor.init]).

— _end note_]

Destroying an object of class type invokes the destructor of the class.

Destroying a scalar type has no effect other than ending the lifetime of the object ([basic.life]).

Destroying an array destroys each element in reverse subscript order.

A declaration that specifies the initialization of a variable, whether from an explicit initializer or by default-initialization, is called the initializing declaration of that variable.

[Note 10:

In most cases this is the defining declaration ([basic.def]) of the variable, but the initializing declaration of a non-inline static data member ([class.static.data]) might be the declaration within the class definition and not the definition at namespace scope.

— _end note_]

9.4.2 Aggregates [dcl.init.aggr]

[Note 1:

Aggregate initialization does not allow accessing protected and private base class' members or constructors.

— _end note_]

The elements of an aggregate are:

When an aggregate is initialized by an initializer list as specified in [dcl.init.list], the elements of the initializer list are taken as initializers for the elements of the aggregate.

The explicitly initialized elementsof the aggregate are determined as follows:

For each explicitly initialized element:

For a non-union aggregate, each element that is not an explicitly initialized element is initialized as follows:

If the aggregate is a union and the initializer list is empty, then

[Example 3:

struct S { int a; const char* b; int c; int d = b[a]; }; S ss = { 1, "asdf" };initializesss.awith 1,ss.bwith "asdf",ss.cwith the value of an expression of the formint{}(that is, 0), and ss.d with the value of ss.b[ss.a](that is, 's'), and instruct X { int i, j, k = 42; }; X a[] = { 1, 2, 3, 4, 5, 6 }; X b[2] = { { 1, 2, 3 }, { 4, 5, 6 } }; a and b have the same value

struct A { string a;int b = 42;int c = -1;};

A{.c=21} has the following steps:

— _end example_]

The initializations of the elements of the aggregate are evaluated in the element order.

That is, all value computations and side effects associated with a given element are sequenced before those of any element that follows it in order.

An aggregate that is a class can also be initialized with a single expression not enclosed in braces, as described in [dcl.init].

The destructor for each element of class type is potentially invoked ([class.dtor]) from the context where the aggregate initialization occurs.

[Note 3:

This provision ensures that destructors can be called for fully-constructed subobjects in case an exception is thrown ([except.ctor]).

— _end note_]

An array of unknown bound initialized with a brace-enclosedinitializer-listcontainingn initializer-clause_s_is defined as havingnelements ([dcl.array]).

[Example 4:

int x[] = { 1, 3, 5 };declares and initializesxas a one-dimensional array that has three elements since no size was specified and there are three initializers.

— _end example_]

An array of unknown bound shall not be initialized with an empty braced-init-list {}.95

[Note 4:

A default member initializer does not determine the bound for a member array of unknown bound.

Since the default member initializer is ignored if a suitable mem-initializer is present ([class.base.init]), the default member initializer is not considered to initialize the array of unknown bound.

[Example 5: struct S { int y[] = { 0 }; }; — _end example_]

— _end note_]

[Note 5:

Static data members, non-static data members of anonymous union members, and unnamed bit-fields are not considered elements of the aggregate.

[Example 6: struct A { int i;static int s;int j;int :17;int k;} a = { 1, 2, 3 };

Here, the second initializer 2 initializesa.jand not the static data memberA​::​s, and the third initializer 3 initializes a.kand not the unnamed bit-field before it.

— _end example_]

— _end note_]

Aninitializer-listis ill-formed if the number ofinitializer-clause_s_exceeds the number of elements of the aggregate.

[Example 7:

char cv[4] = { 'a', 's', 'd', 'f', 0 }; is ill-formed.

— _end example_]

If a member has a default member initializer and a potentially-evaluated subexpression thereof is an aggregate initialization that would use that default member initializer, the program is ill-formed.

[Example 8: struct A;extern A a;struct A { const A& a1 { A{a,a} }; const A& a2 { A{} }; }; A a{a,a}; struct B { int n = B{}.n; }; — _end example_]

If an aggregate class C contains a subaggregate elemente with no elements, the initializer-clause for e shall not be omitted from an initializer-list for an object of typeC unless the initializer-clauses for all elements of C following e are also omitted.

[Example 9: struct S { } s;struct A { S s1;int i1; S s2;int i2; S s3;int i3;} a = { { }, 0, s, 0 }; — _end example_]

When initializing a multi-dimensional array, theinitializer-clause_s_initialize the elements with the last (rightmost) index of the array varying the fastest ([dcl.array]).

[Example 10:

int x[2][2] = { 3, 1, 4, 2 };initializesx[0][0]to3,x[0][1]to1,x[1][0]to4, andx[1][1]to2.

On the other hand,float y[4][3] = { { 1 }, { 2 }, { 3 }, { 4 } };initializes the first column ofy(regarded as a two-dimensional array) and leaves the rest zero.

— _end example_]

Braces can be elided in aninitializer-listas follows.

If theinitializer-listbegins with a left brace, then the succeeding comma-separated list ofinitializer-clause_s_initializes the elements of a subaggregate; it is erroneous for there to be moreinitializer-clause_s_than elements.

If, however, theinitializer-listfor a subaggregate does not begin with a left brace, then only enoughinitializer-clause_s_from the list are taken to initialize the elements of the subaggregate; any remaininginitializer-clause_s_are left to initialize the next element of the aggregate of which the current subaggregate is an element.

[Example 11:

float y[4][3] = { { 1, 3, 5 },{ 2, 4, 6 },{ 3, 5, 7 },};is a completely-braced initialization: 1, 3, and 5 initialize the first row of the arrayy[0], namelyy[0][0],y[0][1], andy[0][2].

Likewise the next two lines initializey[1]andy[2].

The initializer ends early and thereforey[3]s elements are initialized as if explicitly initialized with an expression of the formfloat(), that is, are initialized with0.0.

In the following example, braces in theinitializer-listare elided; however theinitializer-listhas the same effect as the completely-bracedinitializer-listof the above example,float y[4][3] = { 1, 3, 5, 2, 4, 6, 3, 5, 7 };

The initializer forybegins with a left brace, but the one fory[0]does not, therefore three elements from the list are used.

Likewise the next three are taken successively fory[1]andy[2].

— _end example_]

All implicit type conversions ([conv]) are considered when initializing the element with an assignment-expression.

Otherwise, if the element is itself a subaggregate, brace elision is assumed and theassignment-expressionis considered for the initialization of the first element of the subaggregate.

[Note 6:

As specified above, brace elision cannot apply to subaggregates with no elements; aninitializer-clause for the entire subobject is required.

— _end note_]

[Example 12: struct A { int i;operator int();};struct B { A a1, a2;int z;}; A a; B b = { 4, a, a };

Braces are elided around theinitializer-clauseforb.a1.i.

b.a1.iis initialized with 4,b.a2is initialized witha,b.zis initialized with whatevera.operator int()returns.

— _end example_]

[Note 7:

An aggregate array or an aggregate class can contain elements of a class type with a user-declared constructor ([class.ctor]).

Initialization of these aggregate objects is described in [class.expl.init].

— _end note_]

When a union is initialized with an initializer list, there shall not be more than one explicitly initialized element.

[Example 13: union u { int a; const char* b; }; u a = { 1 }; u b = a; u c = 1; u d = { 0, "asdf" }; u e = { "asdf" }; u f = { .b = "asdf" }; u g = { .a = 1, .b = "asdf" }; — _end example_]

[Note 9:

As described above, the braces around theinitializer-clausefor a union member can be omitted if the union is a member of another aggregate.

— _end note_]

9.4.3 Character arrays [dcl.init.string]

An array of ordinary character type ([basic.fundamental]),char8_­t array,char16_­t array,char32_­t array, or wchar_­t array can be initialized by an ordinary string literal, UTF-8 string literal, UTF-16 string literal, UTF-32 string literal, or wide string literal, respectively, or by an appropriately-typed string-literal enclosed in braces ([lex.string]).

Successive characters of the value of the string-literalinitialize the elements of the array.

[Example 1:

char msg[] = "Syntax error on line %s\n";shows a character array whose members are initialized with astring-literal.

Note that because'\n'is a single character and because a trailing'\0'is appended,sizeof(msg)is25.

— _end example_]

There shall not be more initializers than there are array elements.

[Example 2:

char cv[4] = "asdf"; is ill-formed since there is no space for the implied trailing'\0'.

— _end example_]

If there are fewer initializers than there are array elements, each element not explicitly initialized shall be zero-initialized ([dcl.init]).

9.4.4 References [dcl.init.ref]

A variable whose declared type is “reference to T” ([dcl.ref]) shall be initialized.

[Example 1: int g(int) noexcept;void f() { int i;int& r = i; r = 1; int* p = &r; int& rr = r; int (&rg)(int) = g; rg(i); int a[3];int (&ra)[3] = a; ra[1] = i; } — _end example_]

A reference cannot be changed to refer to another object after initialization.

[Note 1:

Assignment to a reference assigns to the object referred to by the reference ([expr.ass]).

— _end note_]

Argument passing ([expr.call])and function value return ([stmt.return]) are initializations.

The initializer can be omitted for a reference only in a parameter declaration ([dcl.fct]), in the declaration of a function return type, in the declaration of a class member within its class definition ([class.mem]), and where theexternspecifier is explicitly used.

[Example 2: int& r1; extern int& r2; — _end example_]

Given types “cv1 T1” and “cv2 T2”, “cv1 T1” is to “cv2 T2” ifT1 is similar ([conv.qual]) to T2, orT1 is a base class of T2.

cv1 T1” is reference-compatiblewith “cv2 T2” if a prvalue of type “pointer to cv2 T2” can be converted to the type “pointer to cv1 T1” via a standard conversion sequence ([conv]).

In all cases where the reference-compatible relationship of two types is used to establish the validity of a reference binding and the standard conversion sequence would be ill-formed, a program that necessitates such a binding is ill-formed.

A reference to type “cv1 T1” is initialized by an expression of type “cv2 T2” as follows:

In all cases except the last (i.e., implicitly converting the initializer expression to the referenced type), the reference is said to bind directly to the initializer expression.

[Note 3:

[class.temporary] describes the lifetime of temporaries bound to references.

— _end note_]

9.4.5 List-initialization [dcl.init.list]

List-initialization is initialization of an object or reference from abraced-init-list.

Such an initializer is called an initializer list, and the comma-separatedinitializer-clause_s_of the initializer-listordesignated-initializer-clause_s_of the designated-initializer-listare called the elements of the initializer list.

An initializer list may be empty.

List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is calleddirect-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization.

[Note 1:

List-initialization can be used

[Example 1: int a = {1}; std::complex<double> z{1,2};new std::vectorstd::string\{"once", "upon", "a", "time"}; f( {"Nicholas","Annemarie"} ); return { "Norah" }; int* e {}; x = double{1}; std::mapstd::string,int\ anim = { {"bear",4}, {"cassowary",2}, {"tiger",7} }; — _end example_]

— _end note_]

A constructor is an initializer-list constructor if its first parameter is of type std​::​initializer_­list<E> or reference tocv std​::​initializer_­list<E> for some type E, and either there are no other parameters or else all other parameters have default arguments ([dcl.fct.default]).

[Note 2:

Initializer-list constructors are favored over other constructors in list-initialization ([over.match.list]).

Passing an initializer list as the argument to the constructor template template<class T> C(T) of a class C does not create an initializer-list constructor, because an initializer list argument causes the corresponding parameter to be a non-deduced context ([temp.deduct.call]).

— _end note_]

The templatestd​::​initializer_­list is not predefined; if the header<initializer_­list> is not imported or included prior to a use ofstd​::​initializer_­list — even an implicit use in which the type is not named ([dcl.spec.auto]) — the program is ill-formed.

List-initialization of an object or reference of type T is defined as follows:

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions ([temp.variadic]), are evaluated in the order in which they appear.

That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clausethat follows it in the comma-separated list of the initializer-list.

[Note 4:

This evaluation ordering holds regardless of the semantics of the initialization; for example, it applies when the elements of theinitializer-list are interpreted as arguments of a constructor call, even though ordinarily there are no sequencing constraints on the arguments of a call.

— _end note_]

An object of type std​::​initializer_­list<E> is constructed from an initializer list as if the implementation generated and materialized ([conv.rval]) a prvalue of type “array of N const E”, where N is the number of elements in the initializer list.

Each element of that array is copy-initialized with the corresponding element of the initializer list, and thestd​::​initializer_­list<E> object is constructed to refer to that array.

[Note 5:

A constructor or conversion function selected for the copy is required to be accessible ([class.access]) in the context of the initializer list.

— _end note_]

If a narrowing conversion is required to initialize any of the elements, the program is ill-formed.

[Example 12: struct X { X(std::initializer_list<double> v);}; X x{ 1,2,3 };

The initialization will be implemented in a way roughly equivalent to this:const double __a[3] = {double{1}, double{2}, double{3}}; X x(std::initializer_list<double>(__a, __a+3));assuming that the implementation can construct an initializer_­list object with a pair of pointers.

— _end example_]

The array has the same lifetime as any other temporary object ([class.temporary]), except that initializing aninitializer_­list object from the array extends the lifetime of the array exactly like binding a reference to a temporary.

[Example 13: typedef std::complex<double> cmplx; std::vector<cmplx> v1 = { 1, 2, 3 };void f() { std::vector<cmplx> v2{ 1, 2, 3 }; std::initializer_list<int> i3 = { 1, 2, 3 };} struct A { std::initializer_list<int> i4; A() : i4{ 1, 2, 3 } {} };

For v1 and v2, the initializer_­list object is a parameter in a function call, so the array created for{ 1, 2, 3 } has full-expression lifetime.

For i3, the initializer_­list object is a variable, so the array persists for the lifetime of the variable.

For i4, the initializer_­list object is initialized in the constructor's ctor-initializer as if by binding a temporary array to a reference member, so the program is ill-formed ([class.base.init]).

— _end example_]

[Note 6:

The implementation is free to allocate the array in read-only memory if an explicit array with the same initializer could be so allocated.

— _end note_]

A narrowing conversion is an implicit conversion

[Note 7:

As indicated above, such conversions are not allowed at the top level in list-initializations.

— _end note_]

[Example 14: int x = 999; const int y = 999;const int z = 99;char c1 = x; char c2{x}; char c3{y}; char c4{z}; unsigned char uc1 = {5}; unsigned char uc2 = {-1}; unsigned int ui1 = {-1}; signed int si1 = { (unsigned int)-1 }; int ii = {2.0}; float f1 { x }; float f2 { 7 }; bool b = {"meow"}; int f(int);int a[] = { 2, f(2), f(2.0) }; — _end example_]