How do I call static members of a template class?
- There is no syntax for specifying that. There's little reason to make
f
a static method anyway. Make it a free function instead. If you must make it a static method for some reason, implement it in terms of a free function and merely call it. - Many compilers will probably do this for you automatically.
No -- if you don't want to use a template argument, don't declare the class with a template parameter. If you need the template argument for other members of the class, but don't need it in f
, then move f
out of the class.
The compiler doesn't know that A<T>::f()
doesn't use type parameter T
. So as it is, you must give the compiler a type any time you use f
.
But when I'm designing a template class and I notice some members/methods don't depend on template parameters, I'll often move those up to a non-template base class.
class A_Base {
public:
static void f();
};
template <class T> class A : public A_Base {
// ...
};
Now A_Base::f()
, A<int>::f()
, and A<double>::f()
really are all the same thing.