开发者

How do I call static members of a template class?

开发者 https://www.devze.com 2023-01-29 18:42 出处:网络
If I have: template <class T> class A { static void f() { // not using temp开发者_开发问答late parameter T

If I have:

template <class T>
class A
{
 static void f()
 {
  // not using temp开发者_开发问答late parameter T
 }
};

In this case A<int>::f() is same as A<double>::f(), but I don't want call A::f() via the template parameter. Is there a syntax that allows calling of f() but doesn't require the template parameter?


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.


  1. 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.
  2. 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.


Sure. Give it an alias by assigning it to a function pointer.


No, actually A<int>::f() is not the same of A<any_other_type>::f(). They are really different. If you want f() to be really independent of the parameter, you can make the template class A to inherit from another class (called sometimes a trait) that offers f() as a static member function:

struct X
{
  static void f() { ...}
};

template <typename T>
struct A : X
{
  ...

Then, you can call either A<type>::f() or X::f().


A cludgy work around (if you don't want to use inheritance and you don't want to make it a non-member function), you could specify a default type for your class:

template <class T = bool>
class A
{
  public:
   static void f() { cout << "f()" <<  endl; }
};

int main(void)
{
  A<>::f();
  return 0;
}

NOTE: this method however is not the same as, A<int>::f() or A<long>::f() etc. but if you have no type dependency in the function - then the above could work...


Each class template instantiation has its own copy of any static data members. The static declaration can be of template argument type or of any defined type.

You must separately define static members. The following example demonstrates this:

template <class T> class K
{
public:
     static T x;
};
template <class T> T K<T> ::x;

int main()
{
    K<int>::x = 0;
}

The statement template T K::x defines the static member of class K, while the statement in the main() function assigns a value to the data member for K .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号