Question: Comparing template singleton variants

Discussion in 'C++' started by John Harrison, Nov 11, 2004.

  1. "Thomas Lorenz" <> 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.
    John Harrison, Nov 11, 2004
    #1
    1. Advertising

  2. John Harrison

    Sharad Kala Guest

    "Thomas Lorenz" <> 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
    Sharad Kala, Nov 11, 2004
    #2
    1. Advertising

  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
    Marek Vondrak, Nov 11, 2004
    #3
  4. 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
    Thomas Lorenz, Nov 11, 2004
    #4
  5. "Marek Vondrak" <> wrote in message
    news:cmvhhg$1oh5$...
    > > > 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
    John Harrison, Nov 11, 2004
    #5
  6. "Thomas Lorenz" <> wrote in message
    news:Xns959F537552D18LorenzThgmxde@198.120.69.11...
    > "John Harrison" <> wrote in
    > news::
    >
    >>
    >> "Marek Vondrak" <> wrote in message
    >> news:cmvhhg$1oh5$...

    > [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
    John Harrison, Nov 12, 2004
    #6
  7. "John Harrison" <> wrote in
    news::

    >
    > "Marek Vondrak" <> wrote in message
    > news:cmvhhg$1oh5$...

    [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
    Thomas Lorenz, Nov 12, 2004
    #7
  8. "Sharad Kala" <> wrote in
    news::

    > 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
    Thomas Lorenz, Nov 12, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Diego

    fgets and variants

    Diego, Apr 2, 2004, in forum: C Programming
    Replies:
    2
    Views:
    461
    Barry Schwarz
    Apr 3, 2004
  2. Replies:
    2
    Views:
    319
    Tony Nelson
    Nov 24, 2005
  3. DanielJohnson

    C compiler variants

    DanielJohnson, Feb 20, 2007, in forum: C Programming
    Replies:
    1
    Views:
    359
    Richard Heathfield
    Feb 20, 2007
  4. Leslaw Bieniasz

    variable number of class method variants

    Leslaw Bieniasz, Jun 26, 2006, in forum: C++
    Replies:
    2
    Views:
    273
    Dervish
    Jun 26, 2006
  5. Adem
    Replies:
    10
    Views:
    605
    Old Wolf
    Nov 12, 2008
Loading...

Share This Page