std::vector‘s initializer list constructor has the form
vector( std::initializer_list<T> init, const Allocator& alloc = Allocator() );
What makes an initialization like std::vector<string> vec{ “foo”, “bar” }; possible? Why does the constructor accept an std::initializer_list<const char*>, even though the std::vectors‘s T is std::string? Where and how does the conversion from std::initializer_list<const char*>to std::initializer_list<string> happen?
I think you should refer to this section of the C++ 17 Standard (11.6.4 List-initialization)
Pay attention to that there is the conversion constructor for the class
std::basic_stringSo in this declaration
at first an object of the type
std::initializer_list<std::string>is constructed from the initializer list and then is used as an initializer for the vector of the typestd::vector<std::string>.If instead you will write
then the compiler will issue an error because there is no implicit conversion from the type
std::initializer_list<const char *>to the typestd::initializer_list<std::string>.That is the compiler determines whether it can build
std::initializer_listfrom a braced initializer list to call the initializer-list constructor.