Why doesn’t my template accept an initializer list

A “thing” like {1,2,3} does not qualify as an expression. It has no type. Therefore, no type deduction is done. But C++0x makes an explicit exception for ‘auto’, so

auto x = {1,2,3};

actually works and decltype(x) will be initializer_list<int>. But this is a special rule that only applies to auto. I guess they wanted to make loops like these

for (int x : {2,3,5,7,11}) {
   ...
}

work since this kind of loop exploits the special rule.

As for solving the problem, you could add an initializer_list<T> overload as a “wrapper”:

template<class T>
inline void outer(initializer_list<T> il) {
   inner(il);
}

I didn’t test this but my current understanding is that it should work.

Leave a Comment