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* (*)()>
uses T
, which is a dependent type; consequently, to access types inside of map<StringID, T* (*)()>
such as iterator
, you need to use the typename
keyword to disambiguate things for the compiler:
typename map<StringID, T* (*)()>::iterator it = m_createFunctions.find(typeID);
See this FAQ for further explanation: What is the template typename
keyword used for?
Note that if you're compiling in C++11 mode, you can use the following simplification instead:
auto it = m_createFunctions.find(typeID);
You will most likely will have errors in GCC also. 'Typename' is necessary in such cases.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.