template static data member definition

A

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.
 
T

titancipher

#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;
}
 
V

velthuijsen

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/ThinkingInCPP2e.html)
 
A

ank

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.
 
A

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.
 
V

velthuijsen

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.
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
 
A

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).
 
V

velthuijsen

ank said:
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>
(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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top