嵌套名称限定符

29

我有一段代码,类似这样:

namespace mymap {
    template <class Key,template <typename T > class Allocator> myownmap {
        typedef pair<const unsigned int, Key> typename _myPair;
        typedef multimap<unsigned int, Key,less<Key> ,Allocator<_myPair> > typename _entriesType;
    }
}

它在MSVC下可成功编译(并运行),但gcc会抱怨无效语法:

.hpp:20: error: expected nested-name-specifier before ‘_myPair’
.hpp:20: error: two or more data types in declaration of ‘_myPair’

我做错了什么?

2
你真的用gcc测试过这段代码吗?在myownmap之前你缺少一个类/结构体,而且“entriesPair”在你的片段中根本没有出现。 - Philipp
你能展示一下 entriesPair 的定义吗? - Peter Alexander
myownmap是什么?它是一个函数还是类? - iammilind
3个回答

40

那里不需要使用 typename,因此不允许在那里使用。

直到模板实际被使用之前,MSVC不能正确解析模板,因此有些错误要等到后面才能发现。


14

"expected nested-name-specifier" 的意思是,在 typename 关键字之后,你应该使用模板参数的某个嵌套名称,例如 typedef typename Key::iterator ...。在您的情况下,您不需要使用 typename


7
typedef pair<const unsigned int, Key> /*typename*/ _myPair;
                                      ^^^^^^^^^^^^ not needed

请查看这里的gcc-4.5输出。(对于myownmapclass还是函数都适用)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接