Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 4
    In some instances, this lazy initialization is not the ideal pattern to follow. One example is if the constructor of the singleton allocates memory from the heap and you wish that allocation to be predictable, for instance in an embedded system or other tightly controlled environment. I prefer, when the Singleton pattern is the best pattern to use, to create the instance as a static member of the class. Commented Jun 17, 2009 at 17:06
  • 4
    For many larger programs, especially those with dynamic libraries. Any global or static object that's none primitive can lead to segfaults/crashes upon program exit on many platforms due to order of destruction issues upon libraries unloading. This is one of the reasons many coding conventions (including Google's) ban the use of non-trivial static and global objects. Commented Jun 17, 2009 at 18:04
  • It seems that the static instance in such implementation has internal linkage, and will have unique and independent copies in different translation unit, which will cause confusing and wrong behavior. But I saw many such implementation, am I missing something? Commented Feb 25, 2017 at 14:12
  • What prevents user from assigning this to multiple objects where compiler behind the scenes uses its own copy constructor? Commented Jul 15, 2019 at 20:46
  • 3
    @FaceBro There are two instances of the keyword static here, but neither of them is problematic from the standpoint of linkage. The first appearance of static is about static member functions, which is nothing to do with linkage. The second appearance of static is about the storage duration of INSTANCE. The object will live in memory for the duration of the program, and it doesn't matter that you cannot access it by name outside of the TU because you access it via the member function instance, which has external linkage. Commented May 23, 2021 at 17:24