0

Suppose I have the following hierarchy:

// the template of the class isn't important for
// the question, I'm using an enum in the real code
enum class State : int { S0, S1 /*... etc */ };

template<State S>
class Base {
public:
    Base(const Vector3f &position) : mPosition(position) {}
protected:
    Vector3f mPosition;
};

template<State S>
class Derived : public Base<S> {
public:
    Derived(const Vector3f &position) : Base<S>(position) {
        // vvvvvvvvv ERROR
        if(mPosition.norm() > 2.0f) { /* do stuff */ }
};

I am aware that I can use this->mPosition as well as Base<S>::mPosition, but I do not understand why. If templates were not involved, it would compile.

I believe this is because at compile time there is no proof that all possible Base<State S> specializations (if they exist) will have an mPosition. Is this correct?

In my particular scenario, I can guarantee that every version of Base and Derived will be compiled and that they will all also have mPosition. Is there anything I can do to use just mPosition in the Derived class?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
svenevs
  • 833
  • 9
  • 24
  • 1
    `mPosition` is a member of `Base` which is a dependent type – read up on dependent types. – ildjarn Jul 17 '16 at 14:46
  • 1
    Unfortunately, no it really isn't possible, since you have a nondependent name in a dependent base class. C++ does not look up nondependent names in dependent base classes. The answer is no. – Alex Huszagh Jul 17 '16 at 14:48
  • Ok thanks for confirming, the linked questions are Deerfield relevant and the phrasing "dependent type" was very helpful. Templates are still new and confusing to me... – svenevs Jul 17 '16 at 14:51

0 Answers0