【发布时间】:2013-07-07 20:57:49
【问题描述】:
clang 3.0 和 g++ 4.8.1 都拒绝以下代码,并在注释中显示错误:
template<typename T>
struct S
{
void f()
{
this->dependent(); // no error: type of 'this' is dependent?
this->Dependent::dependent(); // error: 'Dependent' has not been declared
}
};
根据[basic.lookup.classref]
. 后面的类名或命名空间名。 or -> 运算符在整个后缀表达式的上下文和对象表达式的类范围内查找。
和[temp.dep.expr]
如果封闭的成员函数的类类型是依赖的,
this是类型依赖的。
如果类或命名空间名称Dependent在对象表达式的类的范围内查找*this,并且对象表达式的类是依赖的,是否应该不推迟此查找直到模板被实例化?标准是否规定了正确的行为?
编辑:clang 3.0 接受以下代码,但 g++4.8 给出与上述相同的错误
template<typename T>
struct S
{
T m;
void f()
{
m.dependent();
m.Dependent::dependent();
}
};
【问题讨论】:
-
我看不出
this->Dependent::dependent()怎么可能是一个依赖表达式。它如何依赖于模板参数? -
[temp.dep.expr] "
this是类型相关的,如果封闭成员函数的类类型是相关的"。 -
第二个代码是有意义的,因为它可能依赖于模板而不是第一个。对我来说,第一个和第二个代码在 clang3、g++4.8.1 和 msvc_110 上都编译得很好,除非我当然尝试实例化第一个代码
-
clang 3.0 还是 3.2? clang 3.0 肯定会拒绝第一个代码片段。
标签: c++ templates language-lawyer