fork download
  1. #include <type_traits>
  2. using namespace std;
  3.  
  4. template<typename T>
  5. class singleton
  6. {
  7. public:
  8. singleton () {
  9. static_assert(!std::is_constructible<T>::value, "the constructor is not private");
  10. }
  11.  
  12. static T& instance() {
  13. static T inst;
  14. return inst;
  15. }
  16. };
  17.  
  18. class class_with_public_constr : public singleton<class_with_public_constr>
  19. {
  20. friend class singleton<class_with_public_constr>;
  21. public:
  22. class_with_public_constr() {}
  23. };
  24.  
  25. int main()
  26. {
  27. singleton<class_with_public_constr>::instance();
  28. }
  29.  
Compilation error #stdin compilation error #stdout 0s 3092KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'singleton<T>::singleton() [with T = class_with_public_constr]':
prog.cpp:22:30:   required from here
prog.cpp:9:9: error: static assertion failed: the constructor is not private
         static_assert(!std::is_constructible<T>::value, "the constructor is not private");
         ^
stdout
Standard output is empty