-3

C++ Core Guidelines, I.3: Avoid singletons:

Exception You can use the simplest “singleton” (so simple that it is often not considered a singleton) to get initialization on first use, if any:

X& myX()
{
    static X my_x {3};
    return my_x;
}

Enforcement

  • If a class X has a public static function that contains a function-local static of the class’ type X and returns a pointer or reference to it, ban that.

Why do they ban that?

class X {
 public:
  static X& Get() {
    static X my_x {3};
    return my_x;
  }
};

X::Get() is not worse than myX(). X::Get() is even better than myX() with the stupid prefix "my".

7
  • Then don't use the "my" prefix? Call it X_Get then. It should be more telling that the one "Singleton" they make an exception for is also not considered a "true Singleton." Commented Feb 15, 2024 at 23:09
  • 1
    The X in the question is not a "true Singleton" either. Its constructor is public. Commented Feb 15, 2024 at 23:14
  • 1
    The I.3 rule was just using "my" for illustrative purposes. Not an endorsement or requirement for programmers to use that prefix in their actual code. Commented Feb 15, 2024 at 23:22
  • 1
    Reading the article, the reason given is that Singletons are basically global state in disguise. You should refer to the previous guideline, I.2: Avoid non-const global variables for details on why they suggest mutable global state should be avoided. Commented Feb 15, 2024 at 23:22
  • Since neither of these are singletons, I think the primary idea here is to be picking up on singleton-like practices. In this case, the public static function is worse IMO, because the class is assuming responsibility for the lifetime of its own instances, whereas at least some other function might be providing a more legitimate non-singleton usage. Commented Feb 15, 2024 at 23:23

1 Answer 1

1

The enforcement operates on probability and heuristics.

What does it mean for a singleton to be "simplest" and thereby qualify for the exception? The guidelines are vague on this point (a real example would help), but the characterization "often not considered a singleton" suggests that the the singleton's class does not require it to be used as a singleton. So a class that tries to be a singleton does not qualify as "simplest".

This section of the guidelines gives an example of a class that tries to be a singleton, noting that, inside the class definition, there is often

lots of stuff to ensure that only one Singleton object is created

A static member function meeting the given criteria is commonly part of this "lots of stuff". The existence of such a function is a sign that the class was designed to be used as a singleton, so this class does not qualify for the exception. This is not 100% conclusive, but it holds often enough for the guideline's purposes (apparently). Banning this helps achieve compliance with the guidelines.

In contrast, if a class is not designed as a singleton then it is relatively easy to create a non-member function implementing a singleton pattern. So such a non-member function is allowed (even though there is no guarantee that it qualifies for the exception). Again, this is not 100% conclusive, which is why enforcement is called "Very hard in general."

Could there be a better plan for enforcement? Possibly.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.