2

I try to do the following, I think the example should be self-explaining:

template <class CLASS, class PARAM>
void call(){
  CLASS<PARAM>::do_something();
}

On the angular brackets between CLASS and PARAM on line 3, the compiler says:

error: expected unqualified-id

Can I fix this problem or is it not allowed what I try to do?

Michael
  • 7,407
  • 8
  • 41
  • 84
  • possible duplicate of [What are some uses of template template parameters in C++?](http://stackoverflow.com/questions/213761/what-are-some-uses-of-template-template-parameters-in-c) – awesoon Jul 10 '15 at 09:31
  • How do you expect to *use* this? Perhaps `call()`? Or `call< vector >()`? The latter is possible with changes to the signature of `call`. – Aaron McDaid Jul 10 '15 at 09:58

3 Answers3

4
template <
    template <typename T> class CLASS,
    typename PARAM>
void call()
{
    CLASS<PARAM>::do_something();
}
Nasser Al-Shawwa
  • 3,573
  • 17
  • 27
3

The template parameter CLASS is declared to be a class, or also a typename, I.e. the name of a type.

template<typename X> struct A;

Here A isn't a type, it's a template. To obtain a type, you need to "apply"(*) the template: A<int>.

If you write CLASS<PARAM>, you're trying to apply a type to a type. This won't work. It's like trying to call a value 42(parameter), only on the type level.

So you need to specify that CLASS is something which can be applied, that it's a template:

template <typename T> class CLASS

So, for reference, the complete solution is:

template <template <typename T> class CLASS, class PARAM>
void call(){
  CLASS<PARAM>::do_something();
}

(*) A template is a function on type level: It takes one or more types, and returns a new type.

Daniel Jour
  • 15,896
  • 2
  • 36
  • 63
0

In addition to the answers given by @DanielJour and @Nasser, I want to mention that the name T of the type name for the template template parameter CLASS can be omitted, because it is not used. So, the condensed solution would look like this:

template <template <typename> class CLASS, typename PARAM>
void call()
{
    CLASS<PARAM>::do_something();
}

Reference: Template Template Parameters.

honk
  • 9,137
  • 11
  • 75
  • 83