Consider the function template:
template<typename T> void printme(T&& t) { for (auto i : t) std::cout << i; }
This function expects one parameter of a type with a begin() and end() function.
Question:
Why is the following call illegal?
printme({'a', 'b', 'c'});
Answer:
The call printme({'a', 'b', 'c'}) is illegal because the template argument T cannot be inferred. Without specifying the template argument explicitly, the compiler cannot determine the type of the parameter, as it could be any type with a begin() and end() function. This issue does not arise in the following cases:
The exception to this is the deduction of auto as std::initializer_list
The above is the detailed content of Why is `printme({\'a\', \'b\', \'c\'});` Illegal in C Template Type Deduction?. For more information, please follow other related articles on the PHP Chinese website!