Quick Q: How to write variadic template constructor?
Quick A: use an std::initializer_list
.
Recently on SO:
Writting variadic template constructor
Recently I asked this question but now I would like to expand it. I wrote the following class:
template <class T> class X{ public: vector<T> v; template <class T> X(T n) { v.push_back(n); } template <class T, class... T2> X(T n, T2... rest) { v.push_back(n); X(rest...); } };When creating and object using
X<int> obj(1, 2, 3); // obj.v containts only 1Vector only contains the first value, but not others. I've checked and saw that constructor is called 3 times, so I'm probably creating temp objects and filling their vectors with the rest of the arguments. How do I solve this problem?