why is the initializer_list not deduced by the templated constructor?
The reason is that {1,2,3,4,5,6,7,8,9}
is just a synctatic construct that doesn't have a type. Therefore, the compiler cannot deduce a type T
for this synctatic construct and the first constructor fails.
However, by special Standard rules std::initializer_list<T>
(among other things) can be construct from this synctatic construct and T
can be deduced to int
. Hence the second constructor works.
By constrast with function template argument type deduction, with
auto x = {1,2,3,4,5,6,7,8,9};
the compiler sets the type of x to be std::initializer_list<int>
. There are also special Standard rules that says it must be so. Strictly speaking this is not type deduction because, as said above, {1,2,3,4,5,6,7,8,9}
doesn't have a type to be deduced. (The only type deduction happening here is T = int
in std::initializer_list<T>
.) Here the compiler chooses (it doesn't deduce) the type of x
to be std::initializer_list<int>
. In any case, there's no harm to use the abuse of language of saying that the type of x
is deduced to std::initializer_list<int>
.
Finally, as DyP said in the comments, what you probably want is inheriting all constructors (not only those taking one argument) from the base container class. You can do this by removing all the constructors that you currently have and add just this line to test
:
using container_type::container_type;