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?
` which is a dependent type – read up on dependent types.– ildjarn Jul 17 '16 at 14:46