c++ - Do empty braces call the default constructor or the constructor taking an std::initializer_list? -


the following quote effective modern c++ (page 55):

"suppose use empty set of braces construct object supports default constructor , supports std::initializer_list construction. empty braces mean? etc. rule default construction."

i tried std::array:

std::array<int, 10> arr{}; 

and got warning g++ (version 4.8.2):

warning: missing initializer member ‘std::array<int, 10ul>::_m_elems’

which warning 1 gets when trying construct std::array empty std::initializer_list (see why can initialize regular array {}, not std::array discussion of warning).

so, why isn't above line of code interpreted calling default constructor?

that because std::array aggregate , therefore aggregate initialization performed covered in draft c++11 standard section 8.5.4 [dcl.init.list] says:

list-initialization of object or reference of type t defined follows:

  • if initializer list has no elements , t class type default constructor, object value-initialized.

  • otherwise, if t aggregate, aggregate initialization performed (8.5.1).

    double ad[] = { 1, 2.0 }; // ok int ai[] = { 1, 2.0 }; // error: narrowing  struct s2 {   int m1;   double m2, m3; };  s2 s21 = { 1, 2, 3.0 }; // ok s2 s22 { 1.0, 2, 3 }; // error: narrowing s2 s23 { }; // ok: default 0,0,0 

and can see if not aggregate list goes on , says:

  • otherwise, if t specialization of std::initializer_list, initializer_list object constructed described below , used initialize object according rules initialization of object class of same type (8.5).
  • otherwise, if t class type, constructors considered. applicable constructors enumerated , best 1 chosen through overload resolution (13.3, 13.3.1.7). if narrowing conversion (see below) required convert of arguments, program ill-formed.

we can confirm std::array aggregate section 23.3.2.1 [array.overview]:

an array aggregate (8.5.1) can initialized syntax

array<t, n> = { initializer-list }; 

where initializer-list comma-separated list of n elements types convertible t.

section 8.5.1 referenced 8.5.1 aggregates [dcl.init.aggr] , says:

when aggregate initialized initializer list, specified in 8.5.4, elements of initializer list taken initializers members of aggregate, in increasing subscript or member order [...]

and come full-circle section 8.5.4 started.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -