Clang 3.0 C ++ std :: map<> :: iterator编译错误

时间:2012-02-02 18:03:27

标签: c++ templates clang

我有这段代码:

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();
}

它在Visual Studio 2010和2008上编译得很好,但它不能在Clang 3.0(Xcode)上编译。我认为它在GCC上编译得很好,但我不确定它是否和现在一样。错误“;表达后预期”在这一行:

map<StringID, T* (*)()>::iterator it = m_createFunctions.find(typeID);

你知道为什么吗?

2 个答案:

答案 0 :(得分:4)

VC ++错误地接受了你的代码 - 符合标准的编译器应该在这里给你一个错误。

map<StringID, T* (*)()>使用T,这是一种依赖类型;因此,要访问map<StringID, T* (*)()>内部的类型(例如iterator),您需要使用typename关键字来消除编译器的歧义:

typename map<StringID, T* (*)()>::iterator it = m_createFunctions.find(typeID);

有关详细说明,请参阅此常见问题解答:What is the template typename keyword used for?


请注意,如果您在C ++ 11模式下进行编译,则可以使用以下简化:

auto it = m_createFunctions.find(typeID);

答案 1 :(得分:0)

您很可能也会在GCC中遇到错误。在这种情况下,'Typename'是必要的。