1

This code doesn't compile:

#include<iostream>
using namespace std;
template<class T>struct Base {
    void f(int) { cout << "Base::f() int\n"; }
};
template<class T>struct Derived:Base<T> {
    void g(int i) { f(i); } // should be this->f(i) to pass compile
};
int main(){
    Derived<char> obj;
    obj.g(1); // cfunction short
    return 0;
}

I googled and search on stackoverflow, it says that in the error-line, I should use write either

void g(int i) { this->f(i); }

or

void g(int i) { Base<T>::f(i); }

Yes, they work. I tried none-template version, doesn't need this complex qualifier. As long as both f() and g() doesn't use any template type parameter, I didn't think of a rule on template deduction upon these 2 functions.

Is this some hidden rule behind template resolution that I should add extra qualifier, and why my original code fail to compile?

Immanuel Kant
  • 517
  • 3
  • 8
  • 3
    Does this answer your question? [ISO C++ Standard - rules regarding examining dependent base. Why?](https://stackoverflow.com/q/49013612/2752075) – HolyBlackCat Jun 28 '22 at 04:22
  • *I googled and search on stackoverflow* -- Those links you found didn't explain *why* this is necessary? – PaulMcKenzie Jun 28 '22 at 04:22

0 Answers0