-1

In c++ language, I learned about two phase lookup applied on template.
It said compiler checks twice at template.
But I can't understand the correlation between this pointer and two phase lookup.
So please tell me why removing this pointer (commented region) makes error.

#include <iostream>
using namespace std;

template <class T>
class A {
public:
    void f() {
        cout << "f()\n";
    }
};

template <class T>
class B : public A<T> {
public:
    void g() {

        //this->f();
    }
};

int main()
{
    B<int> b;
    b.g();
}
ChangHyeon
  • 23
  • 3

2 Answers2

1

You need to look at the type of this. It's a B<T>* in your example, which means it depends on T. Therefore the name lookup of f in this->f is done in the second phase, where it's known that T==int and thus this is a B<int>*.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

With

f();

f does not depend on a template parameter and thus looked up only in the template definition context. With

this->f();

f does dependent on a template parameter (because this does as the parent class does) and thus looked up in the template instanciation context.

AProgrammer
  • 51,233
  • 8
  • 91
  • 143