2

What difference in accessibility of variables is there between a normal inheritance and inheritance with templates?

For example the following code will not compile (1)(2):

template<typename T>
struct A {
    T value;
};

template<typename U>
struct B : public A<U> {
    U ret() {
        return value; // Does not compile
    }
};

template<typename U>
struct C : public A<U> {
    U ret() {
        return this->value; // Compiles
    }
};

But the following one do compile (3):

struct A {
    int value;
};

struct B : public A {
    int ret() {
        return value;
    }
};

What part of the standard does create that (rather impractical) difference?

(1): Compiler Explorer

(2): The code can actually compile on MSVC except when MSVC is set to compile in C++20 mode

(3): Compiler Explorer

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116

0 Answers0