#include <iostream>
template <class c> class BASE;
template<>
class BASE<int>
{
int D;
public:
void Foo(){ std::cout << "In BASE<int>Foo()\n"; };
};
template<>
class BASE<float>
{
float D;
float E;
public:
void Foo(){ std::cout << "In BASE<float>Foo()\n"; };
};
template <class c>
class DERIVED
: public BASE<c>
{
public:
// Both the GCC C++ compiler, and the Microsoft
// compiler, accept this syntax:
void RunAny ( ) { BASE<c>::Foo(); };
#ifdef WIN32
// The Microsoft compiler will also accept the following:
void RunMs ( ) { Foo(); };
// It is shorter, and hence more convenient,
// than the version above. However, the GCC compiler
// flags it as a syntax error.
// Is the second (short) version ANSI standard C++?
// What are the applicable sections of the ANSI standard?
#endif
};
void main
(
)
{
DERIVED<int> I;
DERIVED<float> F;
I.RunAny();
F.RunAny();
#ifdef WIN32
I.RunMs();
F.RunMs();
#endif
}
This extract from section 14.6 should shine a little light - template explaination part
of the specification is really tough going:
//start extract
If a name does not depend on a template-parameter (as defined in 14.6.2), a declaration (or set of declarations)
for that name shall be in scope at the point where the name appears in the template definition;
the name is bound to the declaration (or declarations) found at that point and
this binding is not affected by declarations that are visible at the point of instantiation.
[Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void f(char);
template<class T> void g(T t)
{
f(1); // f(char)
f(T(1)); //dependent
f(t); //dependent
dd++; //not dependent
// error: declaration for dd not found
}
void f(int);
double dd;
void h()
{
g(2); //will cause one call of f(char) followed
// by two calls of f(int)
g(’a’); //will cause three calls of f(char)
}