class Base { static bool temp; stat...">

C ++ static variables for a class without references

The following program prints "Here":

#include <iostream> class Base { static bool temp; static bool initTemp() {std::cout<<"Here\n";return true;} }; bool Base::temp = Base::initTemp(); class Derived : public Base {}; int main() {int a;std::cin>>a;} 

The following program "Here" is not printed:

 #include <iostream> template <class T> class Base { static bool temp; static bool initTemp() {std::cout<<"Here\n";return true;} }; template <class T> bool Base<T>::temp = Base<T>::initTemp(); class Derived : public Base<int> {}; int main() {int a;std::cin>>a;} 

In both cases, Base is never referenced. The only difference is that in the second case it is a template class. Can someone explain to me why this is happening. I am using VS 2012.

+7
c ++ static templates
source share
3 answers

In both cases, Base is never referenced.

And it is for this reason that you do not see anything printed on the standard output.

The definition of a static member of a template is not created if you are not using this data item; as member functions, definitions of static data elements of a class template are created on demand.

This is stated in paragraph 14.7.1 / 1 of the C ++ 11 standard:

[...] Implicit instantiation of template template specialization causes implicit declaration creation, but not default definition or arguments, class member functions, member members, highly specialized element enumerations, static data elements and member templates. [...]

Since your client code never refers to Base<>::temp , there is no need to create and initialize it.


As a side note, this signature:

 void main() 

Invalid (standard) C ++. If you want to write portable code, the return type of main() should always be int .

+6
source share

In the first case, you do not create an instance of Base , but you call a static function:

 bool Base::temp = Base::initTemp(); 

In the second case, you never create an instance of template :

 template <class T> bool Base<T>::temp = Base<T>::initTemp(); 

You can explicitly create a template for the Base class, for example:

 template class Base<int>; 

And then you will see "Here."

+2
source share

You cannot create an undefined class variable that you think is associated with a string:

 template <class T> bool Base<T>::temp = Base<T>::initTemp(); 

You cannot allocate a variable of type undefined. You need to write something like:

 Base<int>::temp = value; 

the reasons will be different variables allocated for each type provided, so you cannot have a common static variable for the template class. You will have a separate variable for each type that you create instead of a template.

For illustrator travelers, a complete example is given:

 #include <iostream> template<class T> class X { public: static int v; }; template<class T> int X<T>::v = 0; int main() { X<int>::v = 3; X<char>::v = 2; using namespace std; cout << X<char>::v << endl << X<int>::v; } 

It prints 2 3, which means you cannot have a single variable for all classes that you create for your template.

-3
source share

All Articles