Create a template class for same algorithm different type of member variable I want a template class that defines the same algorithms, but can work on different type of data stored in a member variable.
The exact type of data ideally depends only on a single template parameter. 
I don't want run time polymorphism (like have an argument of a pointer to base class that can accept all derive class).


Below is my current solution using variadic template. Is there a simpler/better way?

#include &lt;vector&gt;
class Data1 {
public:
    Data1(int size, int stride) 
            : _v(size*stride), _size(size), _stride(stride) {}
    int&amp; operator[](int id) {return _v[id*_stride];}
    const int _size;
private:
    const int _stride;
    std::vector&lt;int&gt; _v;
};

class Data2 {
public:
    Data2(int size) : _v(size), _size(size) {}
    int&amp; operator[](int id) {return _v[id];}
    const int _size;
private:
    std::vector&lt;int&gt; _v;
};

template &lt;typename Data, typename... Data_arg&gt;
class Base_template {
public:
    Base_template(Data_arg...data_arg): _data(data_arg...) {}
    void set_all() {
        for (int i = 0; i &lt; _data._size; ++i) _data[i] = _val;
    }
protected:
    int _val;
    Data _data;
};

class Thing1 : public Base_template&lt;Data1, int, int&gt; {
public:
    Thing1(const int size) 
            : Base_template(size, 3) {
        this-&gt;_val = 1;
    }
};
class Thing2 : public Base_template&lt;Data2, int&gt; {
public:
    Thing2(const int size) 
            : Base_template(size) {
        this-&gt;_val = 10;
    }
};

int main() {
    Thing1 t1(100);
    t1.set_all();
    Thing2 t2(10);
    t2.set_all();
}


Notes:


I use variadic template because the data needs to be initialize.
In the simple example above Data2 actually can be replaced by data1 with a stride of 1. But in the actually code, this is not the case.
I'm not sure I understand exactly what you're going for, but I think your Base_template has way too many parameters. You have:

template &lt;typename Data, typename... Data_arg&gt;
class Base_template {
public:
    Base_template(Data_arg...data_arg): _data(data_arg...) {}
    void set_all() {
        for (int i = 0; i &lt; _data._size; ++i) _data[i] = _val;
    }
protected:
    int _val;
    Data _data;
};


but I think all you want is:

template &lt;typename Data&gt;
class Base_template {
public:
    template&lt;typename... Args&gt;
    Base_template(Args&amp;&amp;... args): _data(std::forward&lt;Args&gt;(args)...) {}
    void set_all() {
        for (int i = 0; i &lt; _data._size; ++i) _data[i] = _val;
    }
protected:
    int _val;
    Data _data;
};




Also, notice in passing that your naming convention of "leading underscore" might lead you into conflict with reserved identifiers if you're not super careful. I prefer to use "trailing underscore" for private member variables, just so that I don't have to think about the complicated reserved-identifier rules at all.