Home >Backend Development >C++ >Why Does Template Type Deduction Fail with `initializer_list` in C ?
Template type deduction is a powerful feature of C that allows the compiler to infer the type arguments of a template from the arguments passed to the template. However, in some cases, the compiler is unable to deduce the type arguments, and the template must be explicitly instantiated with the desired type arguments.
Consider the following function:
template<typename T> void printme(T&& t) { for (auto i : t) std::cout << i; }
This function expects a single parameter with a begin()/end()-enabled type. The question is, why is the following code illegal?
printme({'a', 'b', 'c'});
When all of these are legitimate:
printme(std::vector<char>({'a', 'b', 'c'})); printme(std::string("abc")); printme(std::array<char, 3> {'a', 'b', 'c'});
We can even write this:
const auto il = {'a', 'b', 'c'}; printme(il);
Or:
printme<std::initializer_list<char>>({'a', 'b', 'c'});
The first line of code is illegal because the template argument T could not be inferred. If the template argument is explicitly specified, it will work, e.g.:
printme<std::vector<char>>({'a', 'b', 'c'})
Or:
printme<std::initializer_list<char>>({'a', 'b', 'c'})
The other lines of code are legal because the argument has a well-defined type, so the template argument T can be deduced just fine.
The part that may be confusing is that auto will pick the type std::initializer_list
A function parameter for which the associated argument is an initializer list (8.5.4) but the parameter does not have std::initializer_list or reference to possibly cv-qualified std::initializer_list type.
However, with auto, § 7.1.6.4/6 has explicit support for std::initializer_list<>:
if the initializer is a braced-init-list (8.5.4), with std::initializer_list<U>.
The above is the detailed content of Why Does Template Type Deduction Fail with `initializer_list` in C ?. For more information, please follow other related articles on the PHP Chinese website!