mercredi 4 mars 2015

How to set the same type for all arguments in this example?

Just for practice I'm trying to write a variadic template that inputs some values into vector. I wrote the following:



template <class T>
void add(vector<T> *v, T n){
v->push_back(n);
}
template <class T, class... T2>
void add(vector<T> *v, T n, T2... rest){
v->push_back(n);
add(v, rest...);
}


To test these I use the following:



vector<int> vI;
add(&vI, 10, 30, 25);

for (int i = 0; i < vI.size(); i++)
cout << vI[i] << endl;


Everything works as expected, but I'm wondering if the second template could be written in a way that it uses only one type (T or T2) since vector (push_back) expects the same type for all arguments? In fact, I would like to ensure T = T2 all the way.


Aucun commentaire:

Enregistrer un commentaire