TGViewer
C++ - Reddit C++ - Reddit @r_cpp · 229 subscribers
Post #24539 18
CRTP-based Singleton with private construction token — looking for feedback

I experimented with a CRTP-based Singleton that enforces construction via a private token. Curious to hear thoughts.

So, I wanted to implement a singleton in my ECS crtp engine for design and architectural reasons, and I sat down to think about an efficient and crtp-friendly way to do this kind of pattern without necessarily having to alter the original Singleton class contract. The solution is a crtp-based Singleton in which the Derived (the original singleton) inherits from the base Singleton, which exposes the methods required for instantiation and the single exposure of the object. Simply put, instead of boilerplating the class with the classic Singleton code (op = delete), we move this logic and transform it into a proxy that returns a static instance of the derivative without the derivative even being aware of it.

In this way, we manage private instantiation with a struct token which serves as a specific specialization for the constructor and which allows, among other things, making the construction exclusive to objects that have this token.

This keeps the singleton type-safe, zero-cost, CRTP-friendly, and easy to integrate with proxy-based or ECS-style architectures.

Link to the GitHub repo

https://redd.it/1psmcaf
@r_cpp
GitHub GitHub - unrays/crtp-singleton: A type-safe, CRTP-based Singleton pattern in C++ with secure construction and proxy support. A type-safe, CRTP-based Singleton pattern in C++ with secure construction and proxy support. - unrays/crtp-singleton
More from @r_cpp
  1. Oct 9, 2026std::variant, operator== and pattern matching I was thinking, wouldn't adding equality com…
  2. Oct 8, 2026Profiles for simplicity and guarantees - Bjarne Stroustrup - CppCon 2026 https://youtu.be/…
  3. Oct 8, 2026How to Fix autoconf-style Configuration Probing https://build2.org/blog/fix-autoconf.xhtml…
  4. Oct 8, 2026owl – a header-only C++23 HTTP framework where route patterns and handler extractors are c…
  5. Oct 8, 2026Removed by Reddit Removed by Reddit on account of violating the [content policy. ] https:/…
  6. Oct 8, 2026Is there a way to search across all P and N papers throughout standardization history? It…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →