3

Normally when I implement a singleton I make the instance dynamic and have a member function to delete it. In this case, I'm working on an embedded device and I've been told I can't use dynamic memory. It valid for a class to have a static instance of itself within the class declaration, and return it by reference?

(Thread safety is not a concern here.)

class Foo {
private:
    static Foo singleton;

    Foo() { }
    Foo(const Foo &rhs);
    Foo &operator=(const Foo &rhs);

public:
    inline static Foo &Instance(void) {
        return singleton;
    }
};

Foo Foo::singleton;
Evan B.
  • 163
  • 3
  • 14

3 Answers3

15

It is possible to have a static instance, but it is not desirable to have it at class level because it may then happen that it is not yet initialized on access (due to the not completely defined static initialization order). Instead you should use a function-local static:

class Foo {
private:
    Foo() { }
    Foo(const Foo &rhs);
    Foo &operator=(const Foo &rhs);

public:
    inline static Foo &Instance(void) {
        static Foo singleton;
        return singleton;
    }
};

That way it is guaranteed to get initialized the first time the Instance function is called.

celtschk
  • 19,311
  • 3
  • 39
  • 64
2

Yes nothing forbids you to do this. Did you try to compile first?

Also, inline isn't required for a member function defined in the class declaration. Another side note, make sure you understand the implications of using a Singleton (that is rarely a good idea, but in some specific cases can be useful - this is a debate subject but pragmatism always win in the end). Also read this, answers and comments. Experience will help consider use cases better. Just please never allow implicit creation/destruction if you implement a singleton pattern.

Talking about this subject, there is a Singularity library proposed to boost (http://boost.org your standard library complement) that provide features separately : 1. force only one instance 2. optionally make it accessible globally It's available there : https://github.com/icaretaker/Singularity (the doc is in the sources...)

Community
  • 1
  • 1
Klaim
  • 67,274
  • 36
  • 133
  • 188
  • Yes, it compiles, just wondering if anyone could point anything out that's wrong with it. Wow, I didn't realize people were so down on singletons! – Evan B. Jan 10 '12 at 23:29
  • Well you really need to consider other solutions until you got a good reason to use them (and some people say there is none but I disagree). Also, the "singleton" word isn't understood the same way with everyone. For example the OGRE3D library use a Singleton class that implement the exposition of a unique instance, making it "global" in practice, but doesn't allow implicit creation/destruction : you have to write the creation and destruction explicitly to make sure you manage the instance lifetime. It's not exactly Singleton but can be mixed with. – Klaim Jan 10 '12 at 23:35
1

Yes, you can do it.

Note however that there are no guarantees on the order of creation and destruction of static objects across compilation units. Hence, it is best to declare the static in a static accessor member function.

For the same reason, you should be careful not to introduce initialization order dependencies across more than one singleton classes (or other classes with static instances) from different compilation units.

Adam Zalcman
  • 26,643
  • 4
  • 71
  • 92