I’d like to write a function template like that:
template< typename L<T> > // does not work
void do_sth(L<T>& list){
T value = 0;
list.push_back(value);
}
This means, in the template I’d like to work with containers offering “push_back”, but also with the type stored inside that container.
A workaround would be
template< typename T >
void do_sth(typename std::list<T>& list){
T value = 0;
list.push_back(value);
}
// call
std::list<double> list;
do_sth<double>(list);
which is redundant because a) I already specified “double” when declaring “list” and b) the function would not work with a std::vector, although the implementation would perfectly fit.
Does anyone know how to accomplish that?
That’s one of the reasons STL containers provide the
value_typetypedef:Your workaround would also work with a plain call to
do_sth(list);due to template argument deduction.