1

With GCC, we can't access the member in the base class without writing this explicitly, but it works on MSVC, what is going on? Is it because of the CRTP?

#include <iostream>

template<class T>
struct Base {
protected:
    T* a;
};

template<class U>
struct Derived : Base<Derived<U>> {
    void print_a() {
        std::cout << a << std::endl; // doesn't work on GCC
        std::cout << this->a << std::endl; // works on GCC
    }
};

int main() {
    Derived<float> d;
    d.print_a();
}
Silvio Mayolo
  • 41,871
  • 4
  • 57
  • 86
Jojolatino
  • 676
  • 3
  • 8
  • 5
    Having to use `this` is, if I recall correctly, actually mandated by the standard, MSVC is more (too?) tolerant here. Problem is that as `Base` is a template you cannot rely on `a` being available within *every* specialisation – requiring `this` then protects you from *accidentally* using a *global* `a` if the member one does *not* exist. – Aconcagua May 02 '22 at 16:01

0 Answers0