468,720 Members | 1,884 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,720 developers. It's quick & easy.

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 11863
#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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by marcus | last post: by
24 posts views Thread by Steven T. Hatton | last post: by
2 posts views Thread by Scott J. McCaughrin | last post: by
8 posts views Thread by Scott J. McCaughrin | last post: by
3 posts views Thread by Mike - EMAIL IGNORED | last post: by
3 posts views Thread by salem.ganzhorn | last post: by
1 post views Thread by mangalalei | last post: by
9 posts views Thread by bryonone | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.