Partial template specialization with template class

Hello,
I have a generic template class with another template in one of its types. Now I want to specialize one of its methods for a particular (template) class, which leads to a compile error, however.

Here is the example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>

template<typename Type>
class Obj1 {

  public:
      void ID() { printf("Object 1, size = %zu\n", sizeof(Type)); }
};

template<typename Type>
class Obj2 {

  public:
      void ID() { printf("Object 2, size = %zu\n", sizeof(Type)); }
};

template<typename Type, template<typename> class O> class Foo
{
  public:
        inline void ID();

  private:
        O<Type> tO;
};

// Generic
template<typename Type, template<typename> class O> 
void Foo<Type, O>::ID()
{
 tO.ID();
}

// Partial specialization
template<typename Type> 
void Foo<Type, Obj2<Type> >::ID()
{
 printf("Specialization for Obj2\n");
}


int main()
{
  Foo<long, Obj1> foo1;
  Foo<char, Obj1> foo2;
  Foo<char, Obj2> foo3; // Does not work

  foo1.ID();
  foo2.ID();
  foo3.ID();

  return 0;
}


GCC ends with:
:35:27: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Type, template<class> class O> class Foo’
:35:27: error: expected a class template, got ‘Obj2<Type>’


What is wrong with the specialization? Can it even be achieved and how (if so)?

Thank you very much.
Dan T.
Last edited on
Topic archived. No new replies allowed.