ISO C ++ standard - rules for studying the dependent base. What for?

I recently came across a Visual C ++ compiler compatibility mode switch in VS 2017. I read this explanation , which talked about how the switch can prevent compilation of incompatible code

template<typename T> struct B { int f(); }; template<typename T> struct D : B<T> { int g(); }; template<typename T> int D<T>::g() { return f(); // error: should be 'this->f()' } 

In the definition of D :: g, the symbol f is found from the dependent base class B , but standard C ++ does not allow you to examine the dependent base when looking for ads that satisfy the use of f. That is a bug in the source code that Visual C ++ has long been unable to diagnose.

Alright, alright, I get it. Except one. What for?

Why does the standard not allow the study of a dependent base class for f ()? What is the justification for this ban. Does the standard give one?

If B and D were both regular and non-structural structures, f () would be correctly interpreted as a call to a member function of the base class (er ... base struct). So why is this not done when they are templates?

(I'm sure there is a good reason, but with my limited understanding at the moment it just seems annoying. I tried to find on this issue and found at least one question regarding this, but no one with a “why” of it)

+5
source share
1 answer

Because templates can be specialized later. eg.

 template<typename T> struct B { int f(); }; template<typename T> struct D : B<T> { int g(); }; template<typename T> int D<T>::g() { return f(); // the name f won't be looked up when not knowing the exact type of T } template<> struct B<int> { // no function named f for B<int> }; 

So standard C ++ says that independent names are not looked up in dependent base classes.

Adding this-> leads to the fact that the names of the dependent and the dependent names can only be viewed at the time of creating the instance, and at that time the exact basic specialization that needs to be investigated will be known.

Also see Two Phase Search .

+5
source

Source: https://habr.com/ru/post/1275606/


All Articles