14

Usually static members/objects of one class are the same for each instance of the class having the static member/object. Anyways what about if the static object is part of a template class and also depends on the template argument? For example, like this:

template<class T>
class A{
public:
  static myObject<T> obj;
}

If I would cast one object of A as int and another one as float, I guess there would be two obj, one for each type?

If I would create multiple objects of A as type int and multiple floats, would it still be two obj instances, since I am only using two different types?

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
user240137
  • 693
  • 4
  • 10
  • 15

4 Answers4

25

Static members are different for each diffrent template initialization. This is because each template initialization is a different class that is generated by the compiler the first time it encounters that specific initialization of the template.

The fact that static member variables are different is shown by this code:

#include <iostream>

template <class T> class Foo {
  public:
    static int bar;
};

template <class T>
int Foo<T>::bar;

int main(int argc, char* argv[]) {
  Foo<int>::bar = 1;
  Foo<char>::bar = 2;

  std::cout << Foo<int>::bar  << "," << Foo<char>::bar;
}

Which results in

1,2
Yacoby
  • 54,544
  • 15
  • 116
  • 120
  • Since the question is about static members, the example program would be clearer if it did not create any Foo instances. Rather than assigning to and printing out bar1.s, you could use Foo::s instead. – Gareth Stockwell Feb 08 '10 at 12:18
  • @jamesdlin Ug. Fixed. I shouldn't try to flip the logic around in a sentence and then not properly proof read. – Yacoby Feb 08 '10 at 13:22
  • @gareth Valid point and I blame the fact I hadn't had a coffee at that point. Fixed an simplified the code. – Yacoby Feb 08 '10 at 13:33
  • @Yacoby: How about `Foo::bar = 3` in a *different* compilation unit? :) For completeness' sake... – vladr Dec 05 '12 at 21:08
6

A<int> and A<float> are two entirely different types, you cannot cast between them safely. Two instances of A<int> will share the same static myObject though.

villintehaspam
  • 8,540
  • 6
  • 45
  • 76
3

There are as many static member variables as there are classes and this applies equally to templates. Each separate instantiation of a template class creates only one static member variable. The number of objects of those templated classes is irrelevant.

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
1

In C++ templates are actually copies of classes. I think in your example there would be one static instance per template instance.

Thirler
  • 20,239
  • 14
  • 63
  • 92
  • 1
    "C++ templates are actually copies of classes": that does not make sense. 0) there are also template functions and template member functions, 1) define "copy". I guess what you mean is that each class template instantiation represents a new class type. – Sebastian Mach Feb 08 '10 at 13:43
  • Yes unlike Java, C++ internally makes a copy of the class to with the template part filled in. – Thirler Feb 08 '10 at 13:59