Re: Scope of class template static data members
On Jul 16, 5:29 am, Ian Collins <ian-n...@hotmail.com> wrote:
James Kanze wrote:
On Jul 14, 12:55 pm, Ian Collins <ian-n...@hotmail.com> wrote:
James Kanze wrote:
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.
Thanks! The code was originally built with Sun CC, which is a
little lax in that area. Mind you, in this case, it's pretty
obvious where the name originates.
It it. Suppose there's a specialization of the base class for
the type, which doesn't have it? The compiler can't assume that
the template definition which it's already seen corresponds to
the instantiation it will get.
But it must know which base class is used (specialised or not)
at the point of instantiation.
At the point of instantiation. Non-dependent name lookup occurs
at the point of definition, and only at the point of definition.
If you make the name dependent in some way (e.g. "X<N>::called",
or "this->called") name lookup is deferred to the point of
instantiation, and the name in the base class is found.
For example:
template <int N>
struct X { static bool called; };
template <int N> bool X<N>::called;
template <int N> struct Y : X<N>
{
Y() { called = true; }
};
template<> struct X<0> {};
int main()
{
Y<0> y;
}
Will fail to compile.
Yes. And it will fail to compile even if you make the name
dependent.
Unless things have changed in the very latest versions, Sun CC
does not yet (fully) implement two phase name look up. The
"classical" lookup (used by most compilers before the standard)
was only at the point of instantiation---in many ways, a
template was like a macro, with the compiler treating it as just
a sequence of tokens until instantiation. The standard
introducted two phase lookup (very early in the standardization
process, so the implementors really knew what was coming---they
just chose to ignore it) in order to solve two problems: it
allows significantly more error checking at the point of
definition (but requires typename and template in return), and
it prevents "name hijacking", sort of. (IMHO, the cure is worse
than the disease: in my experience, name hijacking was never a
real problem anyway, and the standard solution doesn't really
solve it either. And of course, I would never deliver a
template without having tested it thoroughly, much less without
having instantiated it. Any errors that the compiler can catch
at the definition point wouldn't have gotten through my unit
tests, so it doesn't really matter.)
--
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