14

On Jon's site he has thisvery elegantly designed singleton in C# that looks like this:

public sealed class Singleton
{
    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton instance = new Singleton();
    }
}

I was wondering how one would code the equivalent in C++? I have this but I am not sure if it actually has the same functionality as the one from Jon. (BTW this is just a Friday exercise, not needed for anything particular).

class Nested;

class Singleton
{
public:
  Singleton() {;}
  static Singleton& Instance() { return Nested::instance(); }

  class Nested
  { 
  public:
    Nested() {;}
    static Singleton& instance() { static Singleton inst; return inst; }
  };
};

...


Singleton S = Singleton::Instance();
AndersK
  • 35,813
  • 6
  • 60
  • 86
  • Shouldn't excercises be posted on, I dunno, Monday? Why Friday? I'd think people had something to do on the weekends other than more work – Matthew Scharley Oct 30 '09 at 08:58
  • 4
    Are you looking for native C++ or C++/CLI? I believe the type initialization rules are entirely different for native C++, so I wouldn't expect the same trick to work. Just for the record, this wasn't my own idea - it was pinched from someone else, although I can't remember who :) – Jon Skeet Oct 30 '09 at 08:59
  • you may also want to hide the constructor, destructor, copy & assignment operators. –  Oct 30 '09 at 09:00
  • 31
    Uh oh - if Jon Skeet can't code Jon Skeet's Singleton what can the rest of us hope for??? – Michael Burr Oct 30 '09 at 09:03
  • 1
    I don't know C# so perhaps you could explain why this is better than the standard C++ singleton method. – paxdiablo Oct 30 '09 at 09:03
  • 1
    @paxdiablo: In C#, that would be a thread-safe, lazy loaded singleton. I can't speak for what the standard C++ method is though. – Matthew Scharley Oct 30 '09 at 09:05
  • 3
    C++ < C++0x has no understanding of threads, so looking for a standard thread safe anything in C++ is a vain mission – Pete Kirkham Oct 30 '09 at 09:17
  • 4
    Any discussions of singletons in C++ should at least mention the extensive treatment of policy-driven singleton implementations in Alexandrescu's _Modern C++ Design_ – Steve Gilham Oct 30 '09 at 09:33
  • 8
    And a better exercise would be to figure out how to *avoid* using singletons in a given piece of code ;) – jalf Oct 30 '09 at 09:55

3 Answers3

36

This technique was introduced by University of Maryland Computer Science researcher Bill Pugh and has been in use in Java circles for a long time. I think what I see here is a C# variant of Bill's original Java implementation. It does not make sense in a C++ context as the current C++ standard is agnostic on parallelism. The whole idea is based on the language guarantee that the inner class will be loaded only at the instance of first use, in a thread safe manner. This does not apply to C++. (Also see this Wikipedia entry)

luke
  • 36,103
  • 8
  • 58
  • 81
Vijay Mathew
  • 26,737
  • 4
  • 62
  • 93
9

You'll find a great discussion of how to implement a singleton, along with thread-safety in C++ in this paper.

http://www.aristeia.com/Papers/DDJ_Jul_Aug_2004_revised.pdf

navigator
  • 1,588
  • 1
  • 13
  • 19
  • +1 for mentioning this very good article on the topic by Meyers and Alexandrescu. – sbi Oct 30 '09 at 09:57
  • Alexandrescu also has a whole chapter on implementing singletons in his book "Modern C++ Design". The content is probably similar. – luke Oct 30 '09 at 16:47
1

As far as I am aware, inheritable Singleton behaviour is not possible in C++ or Java, (or at least it wasn't on earlier versions of JDK). This is a C# specific trick. Your subclasses will have to explicitly implement the protocol.

ConcernedOfTunbridgeWells
  • 64,444
  • 15
  • 143
  • 197
  • Am I missing something? I don't think inheritance is entering the picture even in the C# example. – Michael Burr Oct 30 '09 at 09:17
  • It would definitely work in Java - that's where I originally got the idea from. – Jon Skeet Oct 30 '09 at 10:57
  • Perhaps I missed something, the OP gave the impression he was after a generic behaviour. I remember trying to make an inheritable singleton in an early JDK and finding out that Java could not do it; IIRC trad C++ won't either, although you could probably do it with templates. Now I look at the article again it's not specifically mentioned, so maybe he's just looking at how to implement a Singeton. The GOF book might have an example in C++. – ConcernedOfTunbridgeWells Oct 30 '09 at 14:09
  • @ConcernedOfTunbridgeWells: Alexandrescu's "Modern C++ Design" has (as was mentioned elsewhere) treated the subject extensively. Read up on that if you want to know how to do this in C++ generically. – sbi Nov 12 '09 at 23:57