Reputation: 10487
I have:
template<typename TypeA, typename TypeB>
class MyClass
{
public:
static TypeA StaticA;
static TypeB StaticB;
//...other stuff....
};
How do I initialize 'StaticA' (and StaticB)?
If I do this: (in the header file, under the class's declaration but not inside it)
template<>
typename MyClass<TypeA, TypeB>::TypeA MyClass<TypeA, TypeB>::StaticA = TypeA();
Gives me:
'TypeA' was not declared in this scope.
'TypeB' was not declared in this scope.
template argument 1 is invalid
template argument 2 is invalid
And this:
template<typename TypeA, typename TypeB>
typename MyClass<TypeA, TypeB>::TypeA MyClass<TypeA, TypeB>::StaticA = TypeA();
Gives me:
conflicting declaration 'typename MyClass<TypeA, TypeB>::TypeA MyClass<TypeA, TypeB>::StaticA'
'MyClass<TypeA, TypeB>::StaticA' has a previous declaration as 'TypeA MyClass<TypeA, TypeB>::StaticA'
declaration of 'TypeA MyClass<TypeA, TypeB>::StaticA' outside of class is not definition [-fpermissive]
What's the proper way to initialize a templated class's static member that uses a template argument as the type?
Upvotes: 2
Views: 331
Reputation: 10487
Ah, the correct syntax is:
template<typename TypeA, typename TypeB>
TypeA ResourceFactory<TypeA, TypeB>::StaticA = TypeA();
Which in my original problem is:
template<typename TypeA, typename TypeB>
typename MyClass<TypeA, TypeB>:: TypeA MyClass<TypeA, TypeB>::StaticA = TypeA();
^remove^ ^-------remove--------^
I was getting confused because of all the template arguments. =)
Similar to, but not entirely overlapping, this question's answer.
Upvotes: 2