As anyone knows, when a brace initializer is passed directly to a template function, the type cannot be deduced and the compilation fails:
template<typename T>
void f(T)
{
// do something
}
f({ 1,2,3 }); // compilation fails.
But, if you declare a variable using auto and initialize it with a braced initializer, the type deduction works fine and the deduced type is std::initializer_list<T>
.
auto arr = { 1,2,3 }; // works fine
Now the question is why there is such an inconsistency in the C++ standard? What will go wrong, if templates can deduce brace initializers into std::initializer_list<T>
?
Note: There are so many questions on SO from developers that face the deduction of initializer lists for template functions (e.g this one, this one, this one and this). But I cannot find the exact reason behind this decision in the standard.
Aucun commentaire:
Enregistrer un commentaire