c++ - 如果将其用作模板参数,是否需要使用 typename 来限定从属名称?

标签 c++ templates language-lawyer c++03 dependent-name

template<class mapT, class K, class V>
void f(mapT& m, const K& k, const V& v)
{
    pair<mapT::iterator, bool> p = m.insert(make_pair(k, v));
}

MSVC 接受此代码,没有错误或警告。标准对此有何规定?在上例中,我们是否允许(可选)、不允许(禁止)或要求(强制)使用 typename 来限定 T::iterator ?我对 C++03 规则特别感兴趣,尽管如果 11 发生了任何变化,我会很高兴知道。谢谢。

最佳答案

MSVC 不符合要求,代码片段格式不正确;我们必须明确写 typename T::iterator引用类型名称 iterator里面T ,因为它是一个依赖类型

这是编译器中的一个已知错误,请参阅相关错误报告:


标准怎么说? ( 14882-2003 )

14.6.2.2p1 Dependent types [temp.dep.type]

A type is dependent if it is

  • a template parameter,

  • a qualified-id with a nested-name-specifier which contains a class-names that names a dependent type or whose unqualified-id names a dependent type,

  • ...

14.6.2.4p1 Dependent template arguments [temp.dep.temp]

A type template-argument is dependent if the type it specifies is dependent.

关于c++ - 如果将其用作模板参数,是否需要使用 typename 来限定从属名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24158887/

相关文章:

C99 - 关于浮点舍入模式排序的保证

c++ - 在构造函数初始化器中初始化成员数组

c++ - 尝试设置迭代器时出现奇怪的错误

c++ - 指向成员函数的指针和多重继承

C++11 std::begin 不适用于传入模板函数的 int[]

c++ - 修复了带引用的内存布局同步变量

c++ - 了解常量表达式

c++ - 从 Turbo C++(旧)切换到较新的类似编译器

Swift:将通用的 FloatingPoint 值转换为 Int

python - Django 不会扩展到父文件夹中的 base.html