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{
    vector<T> v;
    template <class T>
    X(T n) {
    template <class T, class... T2>
    X(T n, T2... rest) {

When creating and object using

X<int> obj(1, 2, 3);  // obj.v containts only 1

Vector 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?

Add a Comment

Comments are closed.

Comments (0)

There are currently no comments on this entry.