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]
block 引用>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]
block 引用>A type template-argument is dependent if the type it specifies is dependent.
关于c++ - 如果将其用作模板参数,是否需要使用 typename 来限定从属名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24158887/