473,385 Members | 1,343 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,385 software developers and data experts.

Static member variables in template class.

When you have a template class with a static member variable, ie.,

template<typename T>
class handle
{
public:
static void* id() { return &id_; }
private:
static int id_;
};

template<typename T> int handle<T>::id_ = 0;

the compiler is supposed to ensure that for "each" typename T, there
is one and only one copy of the static "id_" variable.

In practice however, how widely is this implemented properly?

I know that in the past some compilers wouldn't event create the
space for the static "id_" variable and it was necessary to explicitly
define them in one spot. For example, it would have been necessary
to enumerate for all T of interest, something like:

int unique<int>::id_ = 0;
int unique<float>::id_ = 0;

Are things better now than they used to be? Are there any modern
compilers which still have strange caveats like this?

What I am hoping to be able to do is to use the address of that static
variable as a unique marker for a type. That is:

template<typename T> void* id1() { return handle<T>::id(); }

cout << "marker<int> = " << id1<int>() << endl;

There is also a simpler version one could write:

template<typename T> void* id2() { static int i = 0; return &i; }

But how portable are such constructs across currently available and
not so current compilers? Can use of shared libraries/dlls complicate
the issue?

Thanks in advance.
Jul 22 '05 #1
4 3112
Graham Dumpleton wrote:
When you have a template class with a static member variable, ie.,

template<typename T>
class handle
{
public:
static void* id() { return &id_; }
private:
static int id_;
};

template<typename T> int handle<T>::id_ = 0;

the compiler is supposed to ensure that for "each" typename T, there
is one and only one copy of the static "id_" variable.

In practice however, how widely is this implemented properly?


I know that gcc 2.95 doesn't do it properly, but 3.0 and up does. I
can't tell anything about other compilers.

Jul 22 '05 #2
Rolf Magnus <ra******@t-online.de> wrote in message news:<c1*************@news.t-online.com>...
Graham Dumpleton wrote:
When you have a template class with a static member variable, ie.,


Currently on darwin (and I saw this on windows at one point) you'll
get a separate instance per shared library. On linux with g++, it
works as you would hope (and possibly expect).

The trick to making this work is to make the static member variable be
a pointer and be prepared to separately initialize each copy to point
to the same single object. For example, the first time you go to
access this static member variable pointer, be prepared for it to be
NULL, and then rather than calling new, go to some centralized library
routine which is responsible for seeing if you're the first one to
access the underlying object you really want. If so, it gets new'd up
and handed back to you, and you make *your* version of the static
member teamplte pointer point at this new object. Otherwise, you're
not the first, so you don't do a new, but you still get back a pointer
and set the static tempalte member pointer to point to the object.

As the C++ lawyers will tell you, "if it involves shared libraries,
it's outside the C++ standard."

Which effectively means just about everything is outside the standard
these days...
Jul 22 '05 #3
de*@pixar.com (David Baraff) wrote in message news:<37**************************@posting.google. com>...
Rolf Magnus <ra******@t-online.de> wrote in message news:<c1*************@news.t-online.com>...
Graham Dumpleton wrote:
When you have a template class with a static member variable, ie.,

Currently on darwin (and I saw this on windows at one point) you'll
get a separate instance per shared library. On linux with g++, it
works as you would hope (and possibly expect).

The trick to making this work is to make the static member variable be
a pointer and be prepared to separately initialize each copy to point
to the same single object. For example, the first time you go to
access this static member variable pointer, be prepared for it to be
NULL, and then rather than calling new, go to some centralized library
routine which is responsible for seeing if you're the first one to
access the underlying object you really want. If so, it gets new'd up
and handed back to you, and you make *your* version of the static
member teamplte pointer point at this new object. Otherwise, you're
not the first, so you don't do a new, but you still get back a pointer
and set the static tempalte member pointer to point to the object.


Unfortunately one doesn't have a string or otherwise to use as the
key in allocating the handle in the centralised repository. This is
because I am trying to have that be automatically bound to the type
of the parameter to the template without special user intervention
to supply a string which identifies the type. If the user was required
to supply the string, wouldn't need the template at all for what I had
in mind.

Anyway, in terms of what I was trying to achieve, I have come across
other problems and so have to do it a totally different and more
verbose way after all.

Thanks though, it confirms my suspicion that it may not have been
portable.
As the C++ lawyers will tell you, "if it involves shared libraries,
it's outside the C++ standard."

Which effectively means just about everything is outside the standard
these days...


And we better make sure we don't top post either. ;-)
Jul 22 '05 #4
gr*****@dscpl.com.au (Graham Dumpleton) wrote in message news:> Unfortunately one doesn't have a string or otherwise to use as the
key in allocating the handle in the centralised repository. This is
because I am trying to have that be automatically bound to the type
of the parameter to the template without special user intervention
to supply a string which identifies the type. If the user was required
to supply the string, wouldn't need the template at all for what I had
in mind.

template <typename T>
struct Cache {
static Data* _ptr;

static Data* GetData() {
if (!_ptr) _ptr =
CentralizedRegistry::GetOrCreateData(typeid(T));
return _ptr;
}
};

The routine CentralizedRegistry::GetOrCreateData() uses the passed in
type_info as a key, and either retrieves a previously created Data*,
or creates one for the first time and stores it under the specified
key. Either way, a Data* is returned. For each type T, the routine
is called at most once per shared library.
Jul 22 '05 #5

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

Similar topics

3
by: Alibek | last post by:
Hello ! I have here template class with static members. and I put definition of the static member in the same ..h file where the class was declared. however, we are getting very stange...
11
by: Roger Leigh | last post by:
The C++ book I have to hand (Liberty and Horvath, Teach yourself C++ for Linux in 21 Days--I know there are better) states that "static member functions cannot access any non-static member...
16
by: Eric | last post by:
I have a static class member variable as follows: struct A { static void Set (int i) { v = i; } static int& Get () { return v; } static int v; }; int A::v; // define A::v in the cpp file
7
by: ank | last post by:
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...
8
by: Markus Henschel | last post by:
Hello, this is a test case of something I just can't explain myself: ...
3
by: Shahin Tavakoli | last post by:
Hi, I'd like to put a static member in a template class, but I don't know the syntax. I've tried out template<typename Type> class Stocker : private vector<Type> { public: static int NB;
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
4
by: aaragon | last post by:
Hi everyone, I have a linking error when using gcc4.2 and static member variables. The class template definition is something around the following: template<> class Element<L2_t: public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.