0

I have a derived class that forwards a template parameter to a base class:

template <class _Unused>
class Base {
  protected:
    static constexpr bool IsBase = true;
};

template <class _Unused>
class Derived : public Base<_Unused> {};

I'd like to access IsBase, marked as protected, in Derived:

template <class _Unused>
class Derived : public Base<_Unused> {
  public:
    static constexpr bool IsDerived = !IsBase; // Why can't I do this?
};

However, the compiler doesn't allow this:

<source>:11:40: error: 'IsBase' was not declared in this scope; did you mean 'Base'?
   11 |     static constexpr bool IsDerived = !IsBase; // Why can't I do this?
      |                                        ^~~~~~
      |                                        Base
Compiler returned: 1

Instead, I need to specialize the parent class:

template <class _Unused>
class Derived : public Base<_Unused> {
  public:
    static constexpr bool IsDerived = !Base<_Unused>::IsBase; // Why is this required?
};

My question is, why is this necessary if I already declared the base class' type to be Base<_Unused> in the class definition?

I expected IsBase to be in scope for Derive<_Unused>, but that's not the case.

Yaneury
  • 1
  • 1

0 Answers0