473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

template static data member definition

ank
Hi,
I was curious about how to define static data member of template class.
Should I put the definition in a separate source file or in the same
header file as its template class?

And when this data will be initialized if it is used across many
translation unit, assume that it has constructor and needs dynamic
initialization?

I have blindly searched for the answer but I still not thoroughly
understand this issue.

Jul 23 '05 #1
7 12429
#include <stdio.h>

template< typename T >
class Test
{
public:
Test( const T & id ) : m_id(id){ }
private:
T m_id;
static int s_count;
};

int Test< float >::s_count = 0;

int main( int argc, char *argv[] )
{
Test< float > test( 1.0f );

return 0;
}

Jul 23 '05 #2
> I was curious about how to define static data member of template class.
Should I put the definition in a separate source file or in the same
header file as its template class?
Same headerfile, after the class.
And when this data will be initialized if it is used across many
translation unit, assume that it has constructor and needs dynamic
initialization?
Same place, it will be initialised only once.
For dynamic initialisation, fill your variable with dummy data then
when the constructor gets called fill in the real values. You might
want to add a static bool and an if statement around the initalisation
in the constructor as a guard against multiple initialisations.
I have blindly searched for the answer but I still not thoroughly
understand this issue.


What specific issue? How static works.
Try 'Thinking in C++ volume 1' chapter 10
(http://www.mindview.net/Books/TICPP/...ngInCPP2e.html)

Jul 23 '05 #3
ank


ve*********@hotmail.com wrote:

Same place, it will be initialised only once.
For dynamic initialisation, fill your variable with dummy data then
when the constructor gets called fill in the real values. You might
want to add a static bool and an if statement around the initalisation
in the constructor as a guard against multiple initialisations.
And what time will it be initialized? Before or after its use or
unspecified just like normal static data member/global object.

I don't know if it could be different from normal static storage
external object.
However, as template code will always be included in every translation
unit (unlike the external object, which is not), I suspect it may be
treated differently in the different compiler.
when the constructor gets called fill in the real values. You might
want to add a static bool and an if statement around the initalisation
in the constructor as a guard against multiple initialisations.


I don't think that static external object will be initialized twice and
it doesn't suffer from multithreading issue anyway, although it can be
too late to initialize that global object before it is used.

Jul 23 '05 #4
ank
Sorry if you feel like I am offending you, that's not my intention, but
this doesn't seem to answer my question.

My point is about the initialization time of the template static data
member across translation unit. Is it just like normal static data
member?

I doubt this because the definition seems to be found before any use of
it although I don't know if the template instantation always follow the
order in which I see inside the code.

Jul 23 '05 #5
> And what time will it be initialized? Before or after its use or
unspecified just like normal static data member/global object.


The same as any class member variable that has been declared static.
when the constructor gets called fill in the real values. You might
want to add a static bool and an if statement around the initalisation
in the constructor as a guard against multiple initialisations.


I don't think that static external object will be initialized twice and
it doesn't suffer from multithreading issue anyway, although it can be
too late to initialize that global object before it is used.


I was wrong in my wording
You do not initialise in the constructor, you assign the real values.
Since you do not initialise you do need the guard to protect against
multiple assignments to mimic the effect of static initialisation.

Oh and you might want to take into account the following:
MyTemplate<int> is a different class from MyTemplate<float>
Which means that you also have two different statics member variables

Jul 23 '05 #6
ank
What do you mean "not initialise in the constructor"?
I really don't understand that.

Constructor is responsible for that task, isn't it?

Anyway, my initial guess is that template static data member is either
initialized before the first use by any module or initialized in some
module (and that may be too late for using it by other external static
object).

Jul 23 '05 #7
ank wrote:
What do you mean "not initialise in the constructor"?
I really don't understand that.

Constructor is responsible for that task, isn't it?
Not in the case of static member variables. The class constructor is
not called before or at the time that the static variable has to
contain a value.
Because you have to give the static variable a value before you have
access to the real values you want to store in it you need to feed the
variable dummy data.
Then in the constructor of the class you assign real values, but to
mimic static initialisation you need to add in a guard so that this
assignment happens only once.

Anyway, my initial guess is that template static data member is either
initialized before the first use by any module or initialized in some
module
A static member of a template class is initialised at the same time any
static member of a non template class is initialised. You have a
guarantee it exists when
template<typename T>
VarType ClassName<T>::VarName = Value;
in the header file is processed.
(and that may be too late for using it by other external static
object).


Existence does not equal accessibility. Unless you have declared the
static variable in the public section of the class, this external
object cannot access your static variable until an object of the class
containing the static variable exists. And even then only through the
appropiate functions of the class.

If you are stuck with static initialisation dependency (That is static
object A has to exist before static object B) I again refer you to
'Thinking in C++' vol 1, chapter 10. That chapter also has strategies
on how to ensure proper initialisation.

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
3
by: marcus | last post by:
I have a class which has one static data member. The .h file containing this class is included from many cpp files. Therefor I have the definition part of the data member in the implementation of...
24
by: Steven T. Hatton | last post by:
In the following code, at what point is S::c fully defined? #include <iostream> using std::cout; using std::endl; using std::ostream; class C { int _v;
2
by: Scott J. McCaughrin | last post by:
The following short program compiles fine but fails to link (both on Borland C++ and GNU g++). In particular, g++ gives this link error: "undefined reference to `VarArrar::funct'". It seems as...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
3
by: Mike - EMAIL IGNORED | last post by:
MyClass { //I have a static member method: void static myMethod(); //and a static data member: static MyType myData; }; //In the .cpp file: void MyClass::myMethod()
3
by: salem.ganzhorn | last post by:
The following code compiles cleanly, but it looks like gcc 3.4.0 does not emit the static data member into the object file (so I get a link error): #include <iostream> template <class Type>...
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.