Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

c++ - What is the rule that allows `this->` to access members of dependent base classes?

As we know, the code below is ill-formed because the member x is in a dependent base class. However, changing x to this->x on the indicated line would fix the error.

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();
}

I would like an explanation of how this behaviour is specified in the standard. According to [temp.dep]/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.

This seems to explain why using x alone fails. The name x is looked up at the point of definition, and the base class scope is not examined. However, what if we use this->x? Now the name x is dependent and its lookup is postponed until instantiation. But the quoted paragraph seems to imply that x should not be found even at instantiation time, since the lookup of x in this->x is still unqualified lookup.

Obviously implementations don't behave this way, and it's widely understood that the base class scope is searched once the template is instantiated.

  1. Have I misinterpreted the quoted paragraph?
  2. Is there a paragraph that specifies the "correct" behaviour?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Class member access expressions (5.2.5. [expr.ref]) don't use unqualified lookup rules, they use class member access lookup rules (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 type C, the unqualified-id is looked up in the scope of class C.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...