What is the rule that allows `this->` to access members of dependent base classes?
如我们所知,下面的代码格式不正确,因为成员
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | template <typename T> struct B { int x; }; template <typename T> struct C : B<T> { void f() { int y = x; // Error! } }; int main() { C<int> c; c.f(); } |
我想解释一下标准中如何规定这种行为。根据[温度差]/3:
In the definition of a class or class template, if a base class depends on a template-parameter, the base class
scope is not examined during unqualified name lookup either at the point of definition of the class template
or member or during an instantiation of the class template or member.
这似乎可以解释为什么单独使用
显然,实现的行为不是这样的,而且广泛理解的是,一旦模板被实例化,就会搜索基类范围。
类成员访问表达式(5.2.5.[expr.ref])不使用非限定的查找规则,它们使用类成员访问查找规则(3.4.5[basic.lookup.classref])。
(2) If the id-expression in a class member access (5.2.5) is an unqualified-id, and the type of the object expression
is of a class typeC , the unqualified-id is looked up in the scope of classC .