fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template <typename T>
  5. class Singleton
  6. {
  7. public:
  8. static T& Get() noexcept
  9. {
  10. static T instance;
  11. return instance;
  12. };
  13.  
  14. protected:
  15. Singleton() = default;
  16. };
  17.  
  18. class Foo : public Singleton<Foo>
  19. {
  20. public:
  21. int t = 0;
  22. private:
  23. Foo() = default;
  24. };
  25.  
  26. int main() {
  27.  
  28. //Foo foo;
  29. Foo::Get().t += 10;
  30.  
  31. return 0;
  32. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘static T& Singleton<T>::Get() [with T = Foo]’:
prog.cpp:29:11:   required from here
prog.cpp:10:16: error: ‘constexpr Foo::Foo()’ is private within this context
       static T instance;
                ^~~~~~~~
prog.cpp:23:6: note: declared private here
      Foo() = default;
      ^~~
stdout
Standard output is empty