Good to solve this little problem. It is pretty simple. vector<int> is a class, so you do not need to declare A<B> in the prototype. You can simply do this:
template<class A> void myFunction(A& list) { typedef typename A::value_type B;
But if you really need to, you can also declare the template argument as a template:
template< template<class> class A, class B > void myFunction(A<B>& list) { typename A<B>::iterator current = list.begin(); typename A<B>::iterator end = list.end(); while (current != end) { current++; } }
But this is not recommended, and most class templates have a set of nested typedefs (such as iterator and value_type in STL containers), so you can get all the necessary information about the type without using this template template parameters. Thus, the first method is usually the preferred and more normal way to execute it (usually there are also fewer problems to make it work, that is, the Compiler tends to “dislike” template template parameters).
In addition, you cannot easily use STL containers with template template parameters, since STL containers have all of these “hidden” template arguments (for example, “dispenser” and “compare” for sorted containers). Thus, you will also need to list all of this, otherwise the compiler will not be able to complete the match. And then you will not have a very “general" function, because you will have to think so much about the STL container that passed because it will only serve one or two types of containers. It is better to use the first method.
Mikael persson
source share