Why do I have to access template base class members through the this pointer?

后端 未结 3 2266
无人及你
无人及你 2020-11-21 04:49

If the classes below were not templates I could simply have x in the derived class. However, with the code below, I have to use this

3条回答
  •  时光取名叫无心
    2020-11-21 05:22

    (Original answer from Jan 10, 2011)

    I think I have found the answer: GCC issue: using a member of a base class that depends on a template argument. The answer is not specific to gcc.


    Update: In response to mmichael's comment, from the draft N3337 of the C++11 Standard:

    14.6.2 Dependent names [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.

    Whether "because the standard says so" counts as an answer, I don't know. We can now ask why the standard mandates that but as Steve Jessop's excellent answer and others point out, the answer to this latter question is rather long and arguable. Unfortunately, when it comes to the C++ Standard, it is often nearly impossible to give a short and self-contained explanation as to why the standard mandates something; this applies to the latter question as well.

提交回复
热议问题