I am teaching myself template programming in C++, so some of my assumptions may be wrong - please correct me if you see any errors.
I am trying to use an STL list as a template parameter to a function. The function is supposed to be used with all sorts of data types, so I have defined the function as template<class T>
rather than template<template<> class T>
in its original declaration. I now want to specialise it to support a template class.
template<class T>
void function(T param)
{
// do something with T
}
template<template <class T, class Allocator> class listPlaceholder>
void function(std::list<T, Allocator> param)
{
// do something with every element in param (I actually need to know it's a list)
std::list<T, Allocator>::iterator current = param.begin();
std::list<T, Allocator>::iterator end = param.end();
do {
function<T>(*current);
} while (++current != end);
}
The problem is that when I try to compile this code (under GCC) it says that T
and Allocator
are not defined in the scope. My primary question is "how do I specialise for template classes?" and secondly, if it is possible, "how do I extract the template template parameters?".
As mentioned previously, I am learning template programming, so obvious solutions are welcome.