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(); 
9
  • 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 :) Commented Oct 30, 2009 at 8:59
  • 31
    Uh oh - if Jon Skeet can't code Jon Skeet's Singleton what can the rest of us hope for??? Commented Oct 30, 2009 at 9:03
  • 3
    C++ < C++0x has no understanding of threads, so looking for a standard thread safe anything in C++ is a vain mission Commented Oct 30, 2009 at 9: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 Commented Oct 30, 2009 at 9:33
  • 8
    And a better exercise would be to figure out how to avoid using singletons in a given piece of code ;) Commented Oct 30, 2009 at 9:55

3 Answers 3

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)

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

+1 for mentioning this very good article on the topic by Meyers and Alexandrescu.
Alexandrescu also has a whole chapter on implementing singletons in his book "Modern C++ Design". The content is probably similar.
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.

4 Comments

Am I missing something? I don't think inheritance is entering the picture even in the C# example.
It would definitely work in Java - that's where I originally got the idea from.
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: 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.