嵌套名称说明符

nested-name-specifier

本文关键字:说明符 嵌套      更新时间:2023-10-16

我有一个类似于的代码

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’

我做错了什么?

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

MSVC在实际使用模板之前不会正确解析模板,因此直到稍后才会发现一些错误。

"预期嵌套名称说明符"意味着在typename关键字之后,您应该使用模板参数的某些嵌套名称,例如typedef typename Key::iterator ...。在您的情况下,您不必使用typename

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

请参阅此处的gcc-4.5输出。(适用于myownmapclass或函数)