3

Google c++ coding-style does not allow non-trivial static objects (and for a reason) and hence no singletons. At the same time singletons do represent reality of application logic.

So what is the correct way to implement singleton functionality google-style:
(a) have static pointers and initialize them on startup as a separate step (e.g. by linked list of initializer/maker classes)
(b) have context holding references to all singleton-like object and pass it with every method (c) have context to be member of every class
(d) something else?

Mike
  • 4,041
  • 6
  • 20
  • 37
uuu777
  • 765
  • 4
  • 21

1 Answers1

1

The "Google C++ Style Guide" does mention "Types representing singleton objects (Registerer)"

You can see an implementation of said registerer in ronaflx/cpp-utility with "util/registerer.h" for function pointers (illustrated here), and util/singleton.h for classic singleton.

The OP points to their own project alex4747-pub/proper_singleton.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • So the actual answer is (a): use static pointers to avoid dealing with destruction issues. – uuu777 Aug 01 '19 at 20:16
  • FYI: I had ust typed in sample code to express (a) approach: https://github.com/alex4747-pub/proper_singleton – uuu777 Jun 21 '20 at 13:29
  • @zzz777 Thank you. I have included your repository in the answer for more visibility. – VonC Jun 21 '20 at 14:04