Initialization of variables in C++ can have several forms:

C++11 introduced a generalized syntax for initialization with a braced initializer list, referred to as braced-init-list. Initialization with braced-init-list is called list initialization. There are two types of list initialization, each having multiple forms (check the above links), but simplified, we can have:

  • direct list initialization: T object {arg1, arg2, ...};
  • copy list initialization: T object = {arg1, arg2, ...};

Prior to C++17 the type for all the following objects (a, b, c and d) is deduced to std::initializer_list<int>. There is no difference between the direct-list-initialization and the copy-list-initialization on the result of the type deduction.

This, however, changed in C++17 that introduced the following rules:

  • for copy list initialization auto deduction will deduce a std::initializer_list<T> if all elements in the list have the same type, or be ill-formed.
  • for direct list initialization auto deduction will deduce a T if the list has a single element, or be ill-formed if there is more than one element.

As a result, the example above changes so that a and c are still std::initializer_list<int> but b is deduced as an int and d is ill-formed, because there is more than one value in the brace-init-list.

For more information about these changes see N3922: New Rules for auto deduction from braced-init-list.

, , , Hits for this post: 701 .

only 1 comment untill now

  1. Gravatar

    So currently clang and gcc apply this change to C++11 and C++14, see Wandbox for latest versions of both: . The proposal noted that this was considered a C++14 defect and clangs status for N3922: says:

    “This is a backwards-incompatible change that is applied to all language versions that allow type deduction from auto (per the request of the C++ committee)”

    Important to note for those using older compilers since there was implementation divergence here for a bit of time and this caused confusion for some as we can see from this Stackoverflow question:

Add your comment now