473,850 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12465
#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*********@hot mail.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<floa t>
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<typena me T>
VarType ClassName<T>::V arName = 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
2002
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 in only objects of class B and skip over the objects of class C. To complicate things, I'm using the vector position (index) as an argument in the invoked member function. It is possible to move the position into the object by adding a data...
3
343
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 the class (in the .cpp file) to avoid getting "multiple defined..." error. In the .cpp file I put the definition outside any method. In the .h file: class .... {
24
2775
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
2153
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 if the static data-member (of type Funct) is regarded as an external, hence unresolvable by 'ld'. If so, why is a static member considered an extern, and what is the remedy?
8
4603
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
3
6392
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
2118
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> class foo { public: foo( Type i )
1
3178
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. And I haved used the sizeof operator to test a class which has a static data member, the class size is all the nonstatic data member except the static data member. In compiler's view, is the static data member constructed after the whole class...
6
3508
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 can be used by static member functions of the base class? Something like virtual static XClass* pXClass; /* pXClass shall be pure virtual, so not static in base class, but
0
9895
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11011
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10351
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9503
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7071
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5735
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4546
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4140
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.