【发布时间】:2011-06-23 15:29:11
【问题描述】:
这里给出了一个关于模板消歧器的问题:
在答案中我们可以阅读:
ISO C++03 14.2/4
当成员模板特化的名称出现在 .或 -> 在后缀表达式中,或在限定标识中的嵌套名称说明符之后,并且后缀表达式或限定标识显式依赖于模板参数(14.6.2),成员模板名称必须是以关键字模板为前缀。否则,该名称被假定为命名一个非模板。
下面是我不太明白的具体例子:
template <class T>
class Base {
public:
template <int v>
static int baseGet() {return v;}
class InnerA {
public:
template <int v>
static int aget() {return v;}
};
class InnerB {
public:
typedef Base BaseType;
typedef BaseType::InnerA OtherType;
template <int v>
static int baseGet() {return BaseType::baseGet<v>();} //(A)
template <int v>
static int aget() {return OtherType::aget<v>();} //(B)
};
};
它显然无法编译。您需要在 (B) 行中使用 template:OtherType::template aget<v>();。
但是,g++ (4.4.3) 和 clang++ (2.9) 都没有抱怨 (A) 行中缺少 template。为什么? BaseType 取决于 T 的类型,不是吗?是那些编译器稍微偏离了标准,还是我误解了标准中的某些内容?
【问题讨论】: