Advertisement
Guest User

Untitled

a guest
Dec 28th, 2012
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <cassert>
  4.  
  5. template<class T>
  6. class singleton
  7. {
  8.     singleton() {}
  9.     ~singleton() { is_destructed = true;  }
  10.  
  11.     static bool is_destructed;
  12. public:
  13.     static T& instance()
  14.     {
  15.         assert(!is_destructed);
  16.         (void)is_destructed; // prevent removing is_destructed in Release configuration
  17.  
  18.         static T inst;
  19.         return inst;
  20.     }
  21.  
  22.     static const T& const_instance() { return instance(); }
  23. };
  24.  
  25. template<class T>
  26. bool singleton<T>::is_destructed = (singleton<T>::instance(), false); // force creating instance before main() is called
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement