views:

118

answers:

3

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?

+2  A: 

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;
yves Baumes
Yes that works but I'd then have to duplicate this code for every type of Foo. This would be a lot of code which surely the compiler can automate through templates...
+3  A: 

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;
Richard Corden
+1  A: 
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>
{
};
Alexey Malistov