0

consider the following code

#include <iostream>

template <size_t N>
struct A {
  int arr[N];
};


template <typename ENUM> 
struct B : public A<ENUM::Size> {
  int foo() { return sizeof(*this); }
};

struct E {
  enum E_ {a, b, c, Size};
};

int main() {
  B<E> b;
  std::cerr << b.foo() << "\n";
}

so far everything compiles just fine, and prints 12. but when i change foo implementation:

int foo() { return arr[0]; } // should inherit arr from parent class A

I'm getting a compilation error:

error: `arr` was not declared in this scope
   int foo() { return i; }

what am I missing? can I make it work?

(obviously, when I change B definition to something silly:

struct B : public A<17> {
  int foo() { return arr[2]; }
};

everything compiles just fine)

what's even more surprising to me is the fact that if I add this line to main():

b.arr[0] = 3;

everything still compiles just fine, so it seems I just can't access A fields from within B

thanks

user2717954
  • 1,822
  • 2
  • 17
  • 28

1 Answers1

1

As @Darhuuk indicated, it's a "derived type" issue. But - you don't have to use this-> necessarily. The following will also work:

int foo() { return A<ENUM::Size>::arr[0]; }

Because it puts you in a derived name lookup context as well.

In fact, even if you forgot about that "derived name lookup" rule, it's always a good idea to try the "full, namespace-and-class, qualified names" of things if their lookup fails. Helps with namespace ambiguities, multiple inheritence, and the common cold too, or so I've heard :-)

einpoklum
  • 118,144
  • 57
  • 340
  • 684