Visibility of Base Template Class Identifiers in Derived Template Class
Consider the following code snippet:
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(); };
When compiled with GCC g 3.4.4 (cygwin), this code compilation fails because NO_ZEROFILL is not visible to the Derived template class. This behavior can be attributed to two-phase lookup in C .
Two-Phase Lookup in C
When the compiler encounters a template declaration, it only performs a preliminary lookup for identifiers used within that template. Since the actual type for T is not determined at this stage, the compiler cannot resolve identifiers that depend on this type, such as Base
In the two-phase lookup process:
In this case, NO_ZEROFILL is not visible during the preliminary lookup because it depends on the unknown type T. As a result, you must explicitly specify Base
The above is the detailed content of Why are Static Members of Base Template Classes Inaccessible in Derived Classes?. For more information, please follow other related articles on the PHP Chinese website!