12 Special member functions [special]

12.6 Initialization [class.init]

12.6.1 Explicit initialization [class.expl.init]

An object of class type can be initialized with a parenthesized expression-list, where the expression-list is construed as an argument list for a constructor that is called to initialize the object. Alternatively, a single assignment-expression can be specified as an initializer using the = form of initialization. Either direct-initialization semantics or copy-initialization semantics apply; see [dcl.init]. Example:

struct complex {
  complex();
  complex(double);
  complex(double,double);
};

complex sqrt(complex,complex);

complex a(1);                   // initialize by a call of
                                // complex(double)
complex b = a;                  // initialize by a copy of a
complex c = complex(1,2);       // construct complex(1,2)
                                // using complex(double,double)
                                // copy/move it into c
complex d = sqrt(b,c);          // call sqrt(complex,complex)
                                // and copy/move the result into d
complex e;                      // initialize by a call of
                                // complex()
complex f = 3;                  // construct complex(3) using
                                // complex(double)
                                // copy/move it into f
complex g = { 1, 2 };           // initialize by a call of
                                // complex(double, double)

 — end example ] [ Note: overloading of the assignment operator ([over.ass]) has no effect on initialization.  — end note ]

An object of class type can also be initialized by a braced-init-list. List-initialization semantics apply; see [dcl.init] and [dcl.init.list]. [ Example:

complex v[6] = { 1, complex(1,2), complex(), 2 };

Here, complex::complex(double) is called for the initialization of v[0] and v[3], complex::complex(double, double) is called for the initialization of v[1], complex::complex() is called for the initialization v[2], v[4], and v[5]. For another example,

struct X {
  int i;
  float f;
  complex c;
} x = { 99, 88.8, 77.7 };

Here, x.i is initialized with 99, x.f is initialized with 88.8, and complex::complex(double) is called for the initialization of x.c.  — end example ] [ Note: Braces can be elided in the initializer-list for any aggregate, even if the aggregate has members of a class type with user-defined type conversions; see [dcl.init.aggr].  — end note ]

Note: If T is a class type with no default constructor, any declaration of an object of type T (or array thereof) is ill-formed if no initializer is explicitly specified (see [class.init] and [dcl.init]).  — end note ]

Note: the order in which objects with static or thread storage duration are initialized is described in [basic.start.init] and [stmt.dcl].  — end note ]