I have this code:
template<typename T>
T* Factory<T>::GetObject(const char* type)
{
StringID typeID(type);
map<StringID, T* (*)()>::iterator it = m_createFunctions.find(typeID);
return it->second();
}
It compiles fine on Visual Studio 2010 and 2008, but it doesn’t compile on Clang 3.0 (Xcode). I think it compiled fine on GCC, but I’m not sure if it was in the same form as now. The error “; expected after expression” is on this line:
map<StringID, T* (*)()>::iterator it = m_createFunctions.find(typeID);
Do you know why?
VC++ accepts your code erroneously — a conformant compiler should give you an error here.
map<StringID, T* (*)()>usesT, which is a dependent type; consequently, to access types inside ofmap<StringID, T* (*)()>such asiterator, you need to use thetypenamekeyword to disambiguate things for the compiler:See this FAQ for further explanation: What is the template
typenamekeyword used for?Note that if you’re compiling in C++11 mode, you can use the following simplification instead: