I have a C++ template class which contains a static variable whose
construction registers the class with a map. Something like this:
template <typename T>
class M {
static Registrar<M> registrar;
};
The constructor of Registrar does the registering when it is initialized.
My problem is, I can't get this to work for a general class M<T>. I can
get it to work for a specific instance, like M<double>, by including the
initialization definition in the .cpp file:
Registrar< M<double> > M<double>::regi strar;
but the general case doesn't work:
template <typename T>
Registrar< M<T> > M<T>::registrar ;
It doesn't matter where I put this code --- in the header or .cpp file
--- the Registrar constructor does not get called.
Can anyone tell me where I should put the definition, and whether I need
to do something special with gcc (v3.3) options?
Drew 7 2469
Drew McCormack wrote: I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this:
template <typename T> class M { static Registrar<M> registrar; };
The constructor of Registrar does the registering when it is initialized.
My problem is, I can't get this to work for a general class M<T>. I can get it to work for a specific instance, like M<double>, by including the initialization definition in the .cpp file:
Registrar< M<double> > M<double>::regi strar;
but the general case doesn't work:
template <typename T> Registrar< M<T> > M<T>::registrar ;
It doesn't matter where I put this code --- in the header or .cpp file --- the Registrar constructor does not get called.
Can anyone tell me where I should put the definition, and whether I need to do something special with gcc (v3.3) options?
All you need to do is use that 'registrar' somehow. If it's not used,
it's not created. 14.7.1/7 says "The implicit instantiation of a class
template does not cause any static data members of that class to be
implicitly instantiated."
What works, as you described, is the _explicit_ instantiation. Implicit
one doesn't.
As to gcc options, please ask in a gcc newsgroup. Compiler options are
off-topic here.
Victor
Victor Bazarov wrote: Drew McCormack wrote:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this:
template <typename T> class M { static Registrar<M> registrar; };
The constructor of Registrar does the registering when it is initialized.
My problem is, I can't get this to work for a general class M<T>. I can get it to work for a specific instance, like M<double>, by including the initialization definition in the .cpp file:
Registrar< M<double> > M<double>::regi strar;
but the general case doesn't work:
template <typename T> Registrar< M<T> > M<T>::registrar ;
It doesn't matter where I put this code --- in the header or .cpp file --- the Registrar constructor does not get called.
Can anyone tell me where I should put the definition, and whether I need to do something special with gcc (v3.3) options?
Hi Victor,
You were right, I needed to use the static variable in my code.
Nonetheless, I am a bit confused as to why this is necessary. Here is why: All you need to do is use that 'registrar' somehow. If it's not used, it's not created. 14.7.1/7 says "The implicit instantiation of a class template does not cause any static data members of that class to be implicitly instantiated."
I do explicitly instantiate the class template: I use M<double> in my
code, for example. What I wasn't doing is using the static variable
explicitly. Should this be necessary, or is explicitly instantiating the
class template enough?
Drew
Victor Bazarov wrote: Drew McCormack wrote:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this:
template <typename T> class M { static Registrar<M> registrar; };
The constructor of Registrar does the registering when it is initialized.
My problem is, I can't get this to work for a general class M<T>. I can get it to work for a specific instance, like M<double>, by including the initialization definition in the .cpp file:
Registrar< M<double> > M<double>::regi strar;
but the general case doesn't work:
template <typename T> Registrar< M<T> > M<T>::registrar ;
It doesn't matter where I put this code --- in the header or .cpp file --- the Registrar constructor does not get called.
Can anyone tell me where I should put the definition, and whether I need to do something special with gcc (v3.3) options?
All you need to do is use that 'registrar' somehow.
One more question: Do you have any suggestions for a clean way to use
the registrar instance in order to force the compiler to create it? At
the moment I am just calling a method of registrar that does nothing
inside the destructor of M<T>, but maybe there is a cleaner way to do it.
Drew
Victor Bazarov wrote: Drew McCormack wrote:
Victor Bazarov wrote:
Drew McCormack wrote:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this:
template <typename T> class M { static Registrar<M> registrar; };
The constructor of Registrar does the registering when it is initialized.
My problem is, I can't get this to work for a general class M<T>. I can get it to work for a specific instance, like M<double>, by including the initialization definition in the .cpp file:
Registrar< M<double> > M<double>::regi strar;
but the general case doesn't work:
template <typename T> Registrar< M<T> > M<T>::registrar ;
It doesn't matter where I put this code --- in the header or .cpp file --- the Registrar constructor does not get called.
Can anyone tell me where I should put the definition, and whether I need to do something special with gcc (v3.3) options?
All you need to do is use that 'registrar' somehow.
One more question: Do you have any suggestions for a clean way to use the registrar instance in order to force the compiler to create it? At the moment I am just calling a method of registrar that does nothing inside the destructor of M<T>, but maybe there is a cleaner way to do it.
What is the purpose of the registrar? Do you intend in the future to query it somehow to find out how many instances/classes have been created in the program? If you do query it, you will get your "use" and it will be instantiated. Until then, put debugging its constructor aside.
Victor
The registrar registers the class in a map, which is used for
serialization. The registrar constructor does this, and it needs to do
it once for each registered class. By including a static instance of
registrar in each class, I intend that the registrar registers the
class, inserting it in the map, before the main program starts.
Victor Bazarov wrote: Drew McCormack wrote:
Victor Bazarov wrote:
Drew McCormack wrote:
I have a C++ template class which contains a static variable whose construction registers the class with a map. Something like this:
template <typename T> class M { static Registrar<M> registrar; };
The constructor of Registrar does the registering when it is initialized.
My problem is, I can't get this to work for a general class M<T>. I can get it to work for a specific instance, like M<double>, by including the initialization definition in the .cpp file:
Registrar< M<double> > M<double>::regi strar;
but the general case doesn't work:
template <typename T> Registrar< M<T> > M<T>::registrar ;
It doesn't matter where I put this code --- in the header or .cpp file --- the Registrar constructor does not get called.
Can anyone tell me where I should put the definition, and whether I need to do something special with gcc (v3.3) options?
Hi Victor, You were right, I needed to use the static variable in my code. Nonetheless, I am a bit confused as to why this is necessary. Here is why:
All you need to do is use that 'registrar' somehow. If it's not used, it's not created. 14.7.1/7 says "The implicit instantiation of a class template does not cause any static data members of that class to be implicitly instantiated."
I do explicitly instantiate the class template: I use M<double> in my code, for example.
Didn't you initially say that you only had a problem with non-specialised (not explicitly instantiated) variations of the template? I took it that you didn't have any problem with 'M<double>::reg istrar' (and you're not supposed to, AFAIUI).
> What I wasn't doing is using the static variable
explicitly. Should this be necessary, or is explicitly instantiating the class template enough?
It's enough to explicitly instantiate the class. But was it the problem you were running into?
Victor
It works if I explicitly instantiate the static variable:
M<double>::regi strar;
But it doesn't work if I just explicitly instantiate the class M:
M<double> mInstance;
The latter does not lead to the static M<double>::regi strar being
created. I thought it should, but it doesn't.
Drew
Drew McCormack wrote: Victor Bazarov wrote:
Drew McCormack wrote:
Victor Bazarov wrote:
Drew McCormack wrote:
> I have a C++ template class which contains a static variable whose > construction registers the class with a map. Something like this: > > template <typename T> > class M { > static Registrar<M> registrar; > }; > > The constructor of Registrar does the registering when it is > initialized. > > My problem is, I can't get this to work for a general class M<T>. > I can get it to work for a specific instance, like M<double>, by > including the initialization definition in the .cpp file: > > Registrar< M<double> > M<double>::regi strar; > > but the general case doesn't work: > > template <typename T> > Registrar< M<T> > M<T>::registrar ; > > It doesn't matter where I put this code --- in the header or .cpp > file --- the Registrar constructor does not get called. > > Can anyone tell me where I should put the definition, and whether I > need to do something special with gcc (v3.3) options?
Hi Victor, You were right, I needed to use the static variable in my code. Nonetheless, I am a bit confused as to why this is necessary. Here is why:
All you need to do is use that 'registrar' somehow. If it's not used, it's not created. 14.7.1/7 says "The implicit instantiation of a class template does not cause any static data members of that class to be implicitly instantiated." I do explicitly instantiate the class template: I use M<double> in my code, for example. Didn't you initially say that you only had a problem with non-specialised (not explicitly instantiated) variations of the template? I took it that you didn't have any problem with 'M<double>::reg istrar' (and you're not supposed to, AFAIUI).
> What I wasn't doing is using the static variable
explicitly. Should this be necessary, or is explicitly instantiating the class template enough? It's enough to explicitly instantiate the class. But was it the problem you were running into?
Victor
It works if I explicitly instantiate the static variable:
M<double>::regi strar;
But it doesn't work if I just explicitly instantiate the class M:
M<double> mInstance;
The latter does not lead to the static M<double>::regi strar being created. I thought it should, but it doesn't.
I think you're confused what is an instantiation of what. When you write
M<double> mInstance;
it's an [explicit] instantiation of the class M<double>, but it's not
an explicit instantiation of the template. For the template, it's very
_implicit_. An explicit instantiation of the template would be
template class M<double>;
Yes, it's supposed to work when you explicitly initialise the static data
member itself. However, it should also work when you instantiate the
template explicitly, without explicit instantiation of the static data
member. Example:
template<class T> struct A { static int sa; }; // template definition
template<class T> int A<T>::sa; // definition of the static member
// the following is the explicit instantiation of A<int>
template struct A<int>; // causes A<int>::sa to be instantiated, no
// matter whether you ever have an object of
// type A<int>
// the following is an explicit instantiation of A<char>::sa
template int A<char>::sa;
// the following is an implicit instantiation of A<double> _and_
// an [explicit] instantiation of an object of A<double> type.
A<double> ad; // does NOT instantiate A<double>::sa
What you need to distinguish here is instantiating a template and that
of the resulting class. When you instantiate a class, you create one
object (instance) of that class, which allocates some memory, etc. But
it doesn't cause any code to be created. When you instantiate a template,
the code for _functions_ is created and also static data members are
created (instantiated).
Victor
Victor Bazarov wrote: Drew McCormack wrote:
Victor Bazarov wrote:
Drew McCormack wrote:
Victor Bazarov wrote:
> Drew McCormack wrote: > >> I have a C++ template class which contains a static variable whose >> construction registers the class with a map. Something like this: >> >> template <typename T> >> class M { >> static Registrar<M> registrar; >> }; >> >> The constructor of Registrar does the registering when it is >> initialized. >> >> My problem is, I can't get this to work for a general class M<T>. >> I can get it to work for a specific instance, like M<double>, by >> including the initialization definition in the .cpp file: >> >> Registrar< M<double> > M<double>::regi strar; >> >> but the general case doesn't work: >> >> template <typename T> >> Registrar< M<T> > M<T>::registrar ; >> >> It doesn't matter where I put this code --- in the header or .cpp >> file --- the Registrar constructor does not get called. >> >> Can anyone tell me where I should put the definition, and whether >> I need to do something special with gcc (v3.3) options? > > > > >
Hi Victor, You were right, I needed to use the static variable in my code. Nonetheless, I am a bit confused as to why this is necessary. Here is why:
> > All you need to do is use that 'registrar' somehow. If it's not used, > it's not created. 14.7.1/7 says "The implicit instantiation of a > class > template does not cause any static data members of that class to be > implicitly instantiated."
I do explicitly instantiate the class template: I use M<double> in my code, for example.
Didn't you initially say that you only had a problem with non-specialised (not explicitly instantiated) variations of the template? I took it that you didn't have any problem with 'M<double>::reg istrar' (and you're not supposed to, AFAIUI).
> What I wasn't doing is using the static variable
explicitly. Should this be necessary, or is explicitly instantiating the class template enough?
It's enough to explicitly instantiate the class. But was it the problem you were running into?
Victor
It works if I explicitly instantiate the static variable:
M<double>::regi strar;
But it doesn't work if I just explicitly instantiate the class M:
M<double> mInstance;
The latter does not lead to the static M<double>::regi strar being created. I thought it should, but it doesn't.
I think you're confused what is an instantiation of what. When you write
M<double> mInstance;
it's an [explicit] instantiation of the class M<double>, but it's not an explicit instantiation of the template. For the template, it's very _implicit_. An explicit instantiation of the template would be
template class M<double>;
Yes, it's supposed to work when you explicitly initialise the static data member itself. However, it should also work when you instantiate the template explicitly, without explicit instantiation of the static data member. Example:
template<class T> struct A { static int sa; }; // template definition template<class T> int A<T>::sa; // definition of the static member
// the following is the explicit instantiation of A<int> template struct A<int>; // causes A<int>::sa to be instantiated, no // matter whether you ever have an object of // type A<int>
// the following is an explicit instantiation of A<char>::sa template int A<char>::sa;
// the following is an implicit instantiation of A<double> _and_ // an [explicit] instantiation of an object of A<double> type. A<double> ad; // does NOT instantiate A<double>::sa
What you need to distinguish here is instantiating a template and that of the resulting class. When you instantiate a class, you create one object (instance) of that class, which allocates some memory, etc. But it doesn't cause any code to be created. When you instantiate a template, the code for _functions_ is created and also static data members are created (instantiated).
Victor
OK, now I get it. Thanks. I wasn't up with the explicit/implicit
definitions. Thanks for making that clear.
Cheers,
Drew This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gianni Mariani |
last post by:
I posted a gcc bug
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13332
here is the text:
-----------------------------------------------------------
This code will compile fine but fail on a linker error.
|
by: gbs |
last post by:
Isn't this legal code to initialize a static member of a template
class?
template <class T>
class TemplatedClassT
{
static int staticMember_;
};
|
by: Kyle |
last post by:
Hi,
I have a library containing some global Variable. However, it seems
that when the library is a static lib the initialization of the global
var does not happen. I could not find any answer in BJ's C++ bible or
on the WEB so far.
Thanks!
If you run sbin (linking with .a), the output is 0 however if
|
by: Robert A Riedel |
last post by:
I have an application that requires a DLL and an executable that uses the
DLL, both of which were implemented in Visual C++ using unmanged code. Both
the executable and the DLL are linked with functions that are stored in a
static library. During initialization of the executable, classes and static
globals within functions linked from the static library appear to be
initialized twice, once when the executable initializes the run-time code,...
|
by: mnowosad |
last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time
an executing code within an AppDomain references a class for the the first
time since the AppDomain was created/loaded, the .NET executes the
assignments done in the class static variables declarations and runs the
static constructor of that class.
So, I expected that, as in ASP.NET web site, for a given Web Service site,
the AppDomain would be initialized upon...
| |
by: ALiX |
last post by:
Hi!
My template class has a const static data member, which is used by the
constructor. It seems that the program does not produce the correct
result. Either it is the fault of the compiler (gcc 4.1) or I need to
learn more about initialization of static members... The code below
captures the problem and is as simple as I could make it:
#include <iostream>
#include <limits>
|
by: Christof Warlich |
last post by:
Hi,
the few lines of code below show different results depending on
the compiler version (gcc-2.95 versus gcc-3.3 and later):
gcc-2.95 first initializes d (line 9) and then t (line 8), as
running the executable yields
initialized d
X constructor
|
by: akomiakov |
last post by:
Is there a technical reason why one can't initialize a cost static non-
integral data member in a class?
|
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 Element_common<L2, Side<2,2 {
public:
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |