Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Question: Comparing template singleton variants

Reply
Thread Tools

Question: Comparing template singleton variants

 
 
John Harrison
Guest
Posts: n/a
 
      11-11-2004

"Thomas Lorenz" <(E-Mail Removed)> wrote in message
news:Xns959E72DCEFAE4LorenzThgmxde@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.


 
Reply With Quote
 
 
 
 
Sharad Kala
Guest
Posts: n/a
 
      11-11-2004

"Thomas Lorenz" <(E-Mail Removed)> 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



 
Reply With Quote
 
 
 
 
Marek Vondrak
Guest
Posts: n/a
 
      11-11-2004
> > 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


 
Reply With Quote
 
Thomas Lorenz
Guest
Posts: n/a
 
      11-11-2004
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
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-11-2004

"Marek Vondrak" <(E-Mail Removed)> wrote in message
news:cmvhhg$1oh5$(E-Mail Removed)...
> > > 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


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-12-2004

"Thomas Lorenz" <(E-Mail Removed)> wrote in message
news:Xns959F537552D18LorenzThgmxde@198.120.69.11.. .
> "John Harrison" <(E-Mail Removed)> wrote in
> news:(E-Mail Removed):
>
>>
>> "Marek Vondrak" <(E-Mail Removed)> wrote in message
>> news:cmvhhg$1oh5$(E-Mail Removed)...

> [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


 
Reply With Quote
 
Thomas Lorenz
Guest
Posts: n/a
 
      11-12-2004
"John Harrison" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

>
> "Marek Vondrak" <(E-Mail Removed)> wrote in message
> news:cmvhhg$1oh5$(E-Mail Removed)...

[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

 
Reply With Quote
 
Thomas Lorenz
Guest
Posts: n/a
 
      11-12-2004
"Sharad Kala" <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments