The following code can be compiled without error:
template <typename T> struct A {
void f() { this->whatever; } // whatever is not declared before
};
int main() {
A<int> a;
}
And I know it's because this
is a type-dependent expression, which makes name lookup for whatever
is postponed until the actual template argument is known. Since member function f()
is never used in this case, so that no instantiation of A<T>::f
exists, and name lookup for whatever
is never performed.
I can understand that this
is type-dependent if the class template has a type-dependent base like:
template <typename T> struct B { T whatever; };
template <typename T> struct A : B<T> {
void f() { this->whatever; }
};
int main() {
A<int> a;
}
When parsing the definition of template class A
, it's impossible to know what's type of its base, which makes this->whatever
potentially legal (B<T>
could has a member named whatever
). On the contrary, I haven't see any potential that this->whatever
would be legal in the first example as soon as member function f
is used somewhere.
So, could this->whatever
be legal at some points in the first example? If not, is there any other reason that this
should be treated as type-dependent expression in that case?