Question

Hi I've got a static member of a templated class that I want defined for a sub group of classes that are templated ie:

template <typename T> 
class FooT
{
private:
 static int ms_id;
};

template <typename T> 
class Foo {};

template<> template<typename T> int FooT< template Foo<T> >::ms_id = 10;

Sadly this throws the following error under gcc 4.1.1

D:\X\Foo.h(98) : error: template argument 1 is invalid

on the line: template<> template<typename T> int FooT< template Foo<T> >::ms_id = 10;

What am I doing wrong is the general concept allowed in the first place?

Was it helpful?

Solution

You can do this by partially specializing an "initializer template":

template <typename T> 
class FooT
{
private:
 static int ms_id;
};

template <typename T> 
class Foo {};

template <typename T>
class GetValue {
  static const int v = 0;
};

template <typename T>
class GetValue< Foo<T> > {
  static const int v = 10;
};

template<typename T> int FooT< T >::ms_id = GetValue<T>::v;

OTHER TIPS

Surely you are not able to put template class as augument in a template instanciation. You need to put a "concrete" class.

For instance , with int:

template <>
int FooT< template Foo< int > >::ms_id = 10; 

or

template<>
int FooT< MyClass >::ms_id = 10; 
template <typename T> class Foo{};

struct MS_ID_TEN
{
protected:
    static int ms_id;
}
int MS_ID_TEN::ms_id = 10; 

template <typename T> struct MS_ID {}
template <typename T> struct MS_ID< Foo<T> > : MS_ID_TEN {};

template <typename T> 
class FooT : public MS_ID<T>
{
};
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top