Question: Comparing template singleton variants

J

John Harrison

Thomas Lorenz said:
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.
 
S

Sharad Kala

Thomas Lorenz said:
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
 
M

Marek Vondrak

static Singleton1 said:
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
 
T

Thomas Lorenz

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
 
J

John Harrison

Marek Vondrak said:
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
 
J

John Harrison

Thomas Lorenz said:
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
 
T

Thomas Lorenz

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
 
T

Thomas Lorenz

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
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top