Consider:
template <typename T>
class Base
{
public:
static const bool ZEROFILL = true;
static const bool NO_ZEROFILL = false;
}
template <typename T>
class Derived : public Base<T>
{
public:
Derived( bool initZero = NO_ZEROFILL ); // NO_ZEROFILL is not visible
~Derived();
}
I am not able compile this with GCC g++ 3.4.4 (cygwin).
Prior to converting these to class templates, they were non-generic and the derived class was able to see the base class’s static members. Is this loss of visibility in a requirement of the C++ spec or is there a syntax change that I need to employ?
I understand that each instantiation of Base<T> will have it’s own static member “ZEROFILL” and “NO_ZEROFILL“, that Base<float>::ZEROFILL and Base<double>::ZEROFILL are different variables, but i don’t really care; the constant is there for readability of the code. I wanted to use a static constant because that is more safe in terms of name conflicts rather than a macro or global.
That’s two-phase lookup for you.
Base<T>::NO_ZEROFILL(all caps identifiers are boo, except for macros, BTW) is an identifier that depends onT.Since, when the compiler first parses the template, there’s no actual type substituted for
Tyet, the compiler doesn’t “know” whatBase<T>is. So it cannot know any identifiers you assume to be defined in it (there might be a specialization for someTs that the compiler only sees later) and you cannot omit the base class qualification from identifiers defined in the base class.That’s why you have to write
Base<T>::NO_ZEROFILL(orthis->NO_ZEROFILL). That tells the compiler thatNO_ZEROFILLis something in the base class, which depends onT, and that it can only verify it later, when the template is instantiated. It will therefore accept it without trying to verify the code.That code can only be verified later, when the template is instantiated by supplying an actual parameter for
T.