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

Question: Comparing template singleton variants

Hello,

a standard template singleton looks basically like this:

template<class T>
class Singleton1 {
private:
Singleton1();
static Singleton1* m_Instance;
public:
Singleton1& getInstance() {
if (m_Instance == 0) {
m_Instance = new Singleton1();
}
return *m_Instance;
}
};
static Singleton1<T>::m_Instance = 0;
This worked fine while used in only one compilation unit, but caused linker
problems. So I came with this variant:

template<class T>
class Singleton2 {
private:
Singleton2();
public:
Singleton2& getInstance() {
static Singleton2 instance;
return instance;
}
};

Compiles fine unit tests are working... but... Usually Singletons are not
implemented that way. Did I overlook something?

Regards
Thomas
Jul 22 '05 #1
7 1484

"Thomas Lorenz" <Lo******@gmx.de> wrote in message
news:Xn***************************@198.120.69.11.. .
Hello,

a standard template singleton looks basically like this:

template<class T>
class Singleton1 {
private:
Singleton1();
static Singleton1* m_Instance;
public:
Singleton1& getInstance() {
if (m_Instance == 0) {
m_Instance = new Singleton1();
}
return *m_Instance;
}
};
static Singleton1<T>::m_Instance = 0;
static is not legal here.


This worked fine while used in only one compilation unit, but caused linker problems. So I came with this variant:

template<class T>
class Singleton2 {
private:
Singleton2();
public:
Singleton2& getInstance() {
static Singleton2 instance;
return instance;
}
};

Compiles fine unit tests are working... but... Usually Singletons are not
implemented that way. Did I overlook something?


Did you put all the code in a header file? Including the definition of
m_Instance? There is no reason that your first version should cause linker
problems if you put all the code in the right places.
Jul 22 '05 #2

"Thomas Lorenz" <Lo******@gmx.de> wrote in message
Hello,

a standard template singleton looks basically like this:

template<class T>
class Singleton1 {
private:
Singleton1();
static Singleton1* m_Instance;
public:
Singleton1& getInstance() {
if (m_Instance == 0) {
m_Instance = new Singleton1();
}
return *m_Instance;
}
};
static Singleton1<T>::m_Instance = 0;
This worked fine while used in only one compilation unit, but caused linker problems. So I came with this variant:

template<class T>
class Singleton2 {
private:
Singleton2();
public:
Singleton2& getInstance() {
static Singleton2 instance;
return instance;
}
};


Singleton2 is also famous as Meyers singleton. But there are issues with
thread safety. Search for double checked locking pattern (DCLP) on google or
even better read about Singletons in Modern C++ Design.

Sharad

Jul 22 '05 #3
> > static Singleton1<T>::m_Instance = 0;

static is not legal here.


This defines storage for m_Instance of Singleton1<T>, where T is a concrete
type name. If it is put in a header file, the storage will be defined as
many as times as the header is included into compilation units. The author
probably wanted to write: template <class T> Singleton1<T>::m_Instance = 0;
which should not cause any troubles.

-- Marek
Jul 22 '05 #4

"Marek Vondrak" <mv******@ceu.cz> wrote in message
news:cm***********@ns.felk.cvut.cz...
static Singleton1<T>::m_Instance = 0;
static is not legal here.


This defines storage for m_Instance of Singleton1<T>, where T is a

concrete type name. If it is put in a header file, the storage will be defined as
many as times as the header is included into compilation units. The author
probably wanted to write: template <class T> Singleton1<T>::m_Instance = 0; which should not cause any troubles.


Right, I missed that the OP had missed out 'template <class T>'.

john
Jul 22 '05 #5
"John Harrison" <jo*************@hotmail.com> wrote in
news:2v*************@uni-berlin.de:

"Marek Vondrak" <mv******@ceu.cz> wrote in message
news:cm***********@ns.felk.cvut.cz...

[Snip]
The author probably wanted to write: template <class T>
Singleton1<T>::m_Instance =

0;
which should not cause any troubles.


Right, I missed that the OP had missed out 'template <class T>'.

john


Well, I abbreviated my "example code" somewhat. You're right - defining
that instance variable is a little bit more complex in "real life" :)

But thanks for the feedback.
Thomas

Jul 22 '05 #6

"Thomas Lorenz" <Lo******@gmx.de> wrote in message
news:Xn***************************@198.120.69.11.. .
"John Harrison" <jo*************@hotmail.com> wrote in
news:2v*************@uni-berlin.de:

"Marek Vondrak" <mv******@ceu.cz> wrote in message
news:cm***********@ns.felk.cvut.cz...

[Snip]
The author probably wanted to write: template <class T>
Singleton1<T>::m_Instance =

0;
which should not cause any troubles.


Right, I missed that the OP had missed out 'template <class T>'.

john


Well, I abbreviated my "example code" somewhat. You're right - defining
that instance variable is a little bit more complex in "real life" :)


Its not, you just put the code that Marek posted into your header file. Did
that not work for you?

If you need help with code it pays to post the real code, otherwise you get
your typos and your abbreviations corrected. It is simply amazing how many
times this needs to be pointed out (and usually after the fact of course).

john
Jul 22 '05 #7
"Sharad Kala" <no*****************@yahoo.com> wrote in
news:2v*************@uni-berlin.de:
Singleton2 is also famous as Meyers singleton. But there are issues
with thread safety. Search for double checked locking pattern (DCLP)
on google or even better read about Singletons in Modern C++ Design.

Sharad


Ah, thanks. Once you know the name of the beast...

Thomas
Jul 22 '05 #8

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

Similar topics

2
by: Sean Dettrick | last post by:
Hi, apologies for yet another Singleton posting. From reading around previous postings etc, I've cobbled together two Singleton template definitions - one that returns a reference, and one that...
7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
6
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...
15
by: Nick Keighley | last post by:
Hi, I found this in code I was maintaining template <class SingletonClass> SingletonClass* Singleton<SingletonClass>::instance () { static SingletonClass _instance; return &_instance; }
6
by: AzizMandar | last post by:
There is probably a better way to do this and if so I'm just as happy to see that way. I have a program where I have factories that each create various objects abstracted from a base class. ...
3
by: YellowMaple | last post by:
Is it possible to have pure virtual functions defined in a template class? I'm trying to do something like this: template <typename T> class Singleton { public: static T& getInstance(void) {...
2
by: ben chang | last post by:
i'm hacking at some code originally written for VC++, trying to port to linux GCC 4. the linker returns multiple-definition errors with a singleton object, which i guess is not a problem in visual...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.