I know how to make the following code work : I just to uncomment the second constructor of Printer.
The idea is simple : I want to write a constructor / function that can take several arguments stored in some abract data structure that I can iterate. I want it to work at least with vectors and lists (it does) but also with initializer list (and it doesn't).
I use the following simple syntax (maybe a bit more generic than I would like, I don't use template templates) so I don't have to write a variadic template in order to handle the allocator types of std::
#include <iostream>
#include <vector>
#include <list>
#include <initializer_list>
using namespace std;
struct Printer
{
template<class Container>
Printer(const Container& cont)
{
for(const auto & e : cont)
cout << e << " ";
cout << endl;
}
//template<class T>
//Printer(const initializer_list<T> & l)//why should I ?
//{
// for(const T & e : l)
// cout << e << " ";
// cout << endl;
//}
};
int main()
{
vector<int> v = {1,2,3};
Printer pv(v);
Printer pv2 = v; //ok : not explicit
list<char> l = {'a', 'b', 'c'};
Printer pl(l);
Printer pl2 = l; //ok : not explicit
Printer pi1 = {4,5,6};
Printer pi2 ({4,5,6}); //same as above if explicit
}
But why should I explicitely write a specific constructor for initializer list ? The error is "could not convert ‘{4, 5, 6}’ from ‘’ to ‘Printer’".
Basically, what it tells is that the substitution does not work with the initializer list. But why ?