Re: Scope of class template static data members
On Jul 14, 10:44 am, Ian Collins <ian-n...@hotmail.com> wrote:
Consider the following snippet:
struct X { static bool called; };
struct Y : X
{
Y() { called = true; } // g++ is happy with this
};
template <int N>
struct XT { static bool called; };
template <int N> bool XT<N>::called;
template <int N>
struct YT : XT<N>
{
YT() { called = true; }/ / g++ is unhappy with this
Normal. The expression is not dependent, so the compiler does
name lookup exclusively at the point of definition. At which
time, it hasn't the slightest idea what might be in XT<N> (since
it is a dependent base). See =A714.6.2/3: "In the definition of a
class template or a member of a class template, if a base class
of the class template depends on a template-parameter, the base
class scope is not examined during unqualified name lookup
either at the point of definition of the class template or
member or during an instantiation of the class template or
member." (Since called is not dependent here, the only lookup
is at the point of definition.)
You have to write either "this->called = true", or
"XT<N>::called = true".
};
int main()
{
YT<0> yt;
}
g++ is unhappy with the assignment to called in YT's constructor:
/tmp/x.cc: In constructor 'YT<N>::YT()':
/tmp/x.cc:16: error: 'called' was not declared in this scope
But it's happy with the non-template version.
Is this a compiler issue, or have I missed something in the standard?
The fact that a dependent base class does not participate in
non-dependent name lookup.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34