In C ++ 11, curly braces {} are used to uniformly initialize. Any object that can be initialized can be initialized with curly braces. Preference is given to constructors that accept initializer_list<T> , therefore, if initialization of the italic bracket can correspond to more than one constructor, and one of them accepts initializer_list , which will be selected. However, if the initializer_list constructors do not match, other constructors are also considered.
For instance:
vector<int> two_elems{5,10};
In your example, you initialize string with char const[13] . This will match initializer_list<char const*> if string has such a constructor but does not match initializer_list<char> . Thus, this parameter is mapped to other constructors, and a constructor accepting char const* is the best match.
Note that because of the ambiguity, it is best not to initialize containers (e.g. vector , set , list or even string )) using parenthesis initialization unless you intend to use initializer_list constructors. For example, there is no way to call the vector<size_t> constructor, which takes size_t, T using the brace-initialization function, since this argument list will also match the initializer_list constructor. As another example:
vector<char const*> vec_of_strs{ 5, 0 };
It is not immediately obvious that the two lines above cause very different constructors, and if the type of one was changed to another during maintenance (or the code that occurred in the template), programmers may be very surprised by the sudden behavior of the change.
source share