I ran into this situation when trying to run down a subtle bug in some code.
template<typename T>
struct S
{
int f(){ return this->r; }
int g(){ return r; }
};
The compiler flags an error on the definition of S::g
, even when I never instantiate a specialization of the template, which is actually what I expected: The name r is unqualified and is not declared anywhere in scope.
What puzzles me is that an error is not flagged on the definition of S::f
until I instantiate a specialization, say, CT<int>f
, and call its f
.
In other words, it seems like the compiler regards this
as a pointer to a dependent type instead of a pointer to the current instantiation.
Is this correct? (My compiler is g++ 8.1.0). If g++ is doing the right thing, can you explain it?