Shivanand
Shivanand

Reputation: 144

Both adding and not adding 'typename' to templated static member initialization gives errors

While initializing a templated static member, both adding and not adding 'typename' give errors. I am not sure where I am wrong here. Below is a complete, minimal example:

myclass.h

template <typename T> class MyClass{
    public:    
        static T G;
};

Case 1: myclass.cpp

#include "myclass.h" 

template<typename T> MyClass<T>::G = 25;

Output 1:

error: need 'typename' before 'MyClass<T>::G' because 'MyClass<T>' is a dependent scope

Case 2: myclass.cpp

#include "myclass.h" 

template<typename T> typename MyClass<T>::G = 25;

Output 2:

error: expected unqualified-id before '=' token

Could you please help me fix this?

Upvotes: 1

Views: 34

Answers (1)

Amadeus
Amadeus

Reputation: 10685

Nothing much to say, just:

template<typename T> T MyClass<T>::G = 25;

You can see it working here on coliru

Upvotes: 3

Related Questions