I have the following code snippet, but it does not work. the dataMap member is supposed to contain a callback function that takes a T& and the T to pass to the callback at the appropriate time. The template member initialization fails (with g++ 4.7.2) with: error: need ‘typename’ before ‘MyClass<T>::DataMap’ because ‘MyClass<T>’ is a dependent scope. I tried sticking typename in where it says, but then got a different error: error: expected primary-expression before ‘;’ token.
Is it possible to do it this way, or do I need to remove the typedefs from the initialization? I tried going down that path, but it got illegible quickly and spit out even more errors.
template <typename T> class MyClass
{
public:
typedef void(*CallbackType)(T&);
typedef std::unordered_map<int, std::pair<T, CallbackType>/**/> DataMap;
static DataMap dataMap;
...
};
template <typename T> MyClass<T>::DataMap MyClass<T>::dataMap = MyClass<T>::DataMap;
The compiler is correctly suggesting you that you might want to include the
typenamekeyword, becauseDataMapis a qualified dependent name inMyClass<T>::DataMap. The static member definition should look like this:The member will be default constructed, so there is no need to copy-initialize it. Thus, it is enough to omit the
= MyClass<T>::DataMappart (which gives you troubles because you forgot the parentheses afterDataMapon the right side of the copy-initialization):