fork(2) 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. private:
  15. Singleton() = default;
  16. };
  17.  
  18. class Foo : public Singleton<Foo>
  19. {
  20. public:
  21. int t = 0;
  22. };
  23.  
  24. int main() {
  25.  
  26. //Foo foo;
  27. Foo::Get().t += 10;
  28.  
  29. return 0;
  30. }
Compilation error #stdin compilation error #stdout 0s 4796KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of ‘static T& Singleton<T>::Get() [with T = Foo]’:
prog.cpp:27:7:   required from here
prog.cpp:10:12: error: use of deleted function ‘Foo::Foo()’
   static T instance;
            ^~~~~~~~
prog.cpp:18:7: note: ‘Foo::Foo()’ is implicitly deleted because the default definition would be ill-formed:
 class Foo : public Singleton<Foo>
       ^~~
prog.cpp:18:7: error: ‘constexpr Singleton<T>::Singleton() [with T = Foo]’ is private within this context
prog.cpp:15:2: note: declared private here
  Singleton() = default;
  ^~~~~~~~~
stdout
Standard output is empty