[英]Clang 3.0 C++ std::map<>::iterator compilation error
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). 它可以在Visual Studio 2010和2008上很好地编译,但是不能在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. 我认为它在GCC上可以很好地编译,但是我不确定它的格式是否与现在相同。 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. VC ++接受你的代码错误-一个符合的编译器应该在这里给你一个错误。
map<StringID, T* (*)()>
uses T
, which is a dependent type; map<StringID, T* (*)()>
使用T
,它是一个依赖类型; 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: 因此,要访问map<StringID, T* (*)()>
内部的类型map<StringID, T* (*)()>
例如iterator
,您需要使用typename
关键字来消除编译器的歧义:
typename map<StringID, T* (*)()>::iterator it = m_createFunctions.find(typeID);
See this FAQ for further explanation: What is the template typename
keyword used for? 有关更多说明,请参见此FAQ: 模板typename
关键字是做什么用的?
Note that if you're compiling in C++11 mode, you can use the following simplification instead: 请注意,如果您在C ++ 11模式下进行编译,则可以使用以下简化方式:
auto it = m_createFunctions.find(typeID);
You will most likely will have errors in GCC also. 您很可能也会在GCC中出错。 'Typename' is necessary in such cases. 在这种情况下,必须使用“类型名”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.