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.
#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:
inlinevoid 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)?