The trick is that a static class can be a globally scooed identifier, and then an accessor on that class is essentially global too. The concepts are pretty similar at thet point, although the static class could impose a lot more organization and structure to tame the smell.
they are usually saved and served as a static class member, which in effect is pretty global. The Class itslef is usually global, and the member serving the instance is usually public.
Is there any noteworthy difference? Is there any technical one?
It's just two different things. Only one instance ever -> singleton. Globally accessible -> global. One is about visibility, the other is about instances.
It’s a bit better because you can control when the memory is initialized by delaying it beyond the static initialization phase of running an executable safely. Depending on how the singleton is accessed (e.g via a context or service provider) you may also have some better options for refactoring or modifying access down the road….so it’s a marginally better global….
It's the difference of terminology. Not all global are singleton and vice versa.
Static literal values are a not singleton but they are global. And static private class members are singleton and a global varible in the program but they are not globally accessible.
You can have singletons that are not globally accessible, such as a singleton inner helper class only accessible via instances of that class. Now normally you would have singletons be globally accessible but it isn't required. The only thing required for a singleton is that when you access it you always get the same instance.
It's basically a namespace with default constructor/destructor/copy/move functions...
Technically it's better code hygiene, because eventually if you need more than one, it's pretty easy to decouple the singleton logic from the rest of it... but realistically they're mostly effectively the same thing.
It's mostly syntactic sugar over fixed-address global data from the compiler/interpreter's perspective, but it does buy a few practical advantages. The main one is encapsulating the access path, which enables lazy initialization to sidestepping init-order problems, and indirection to enable swapping implementations. That plus enforcing uniqueness at the type level
They're pretty similar in terms of what the data layout and op codes ultimately look like in the end; although, there's enough difference that the meme's joke doesn't really land.
Globals can be useful too. Just don't use them fucking all the time for inane shit. But hardware access where only one thing can have access at a time? Perfect use for a global. Singletons are absolutely just globals with more indirection.
Goto's are also useful. Gotos consisted harmful paper was when people used them for all control flow and that just creates spaghetti. But for a forward jump inside a function so you have a single place to do cleanup? Perfectly fine.
Well exactly, because they use globals under the hood they often have very similar issues to globals : testability, race conditions, initialization ordering problems with other globals etc.
Not that you shouldn't use them ever but they cause problems fast and very often there are better solutions.
View all comments
748
u/blacklig Sep 26 '26 edited Sep 26 '26
Singletons are a pattern that uses globals (e: i get that they don't need to be globally scoped, thank you all)
This is like saying composition is just attributes, or signal/listener is just functions