fork download
  1. #include <iostream>
  2. #include<new>
  3. #include<vector>
  4.  
  5. class Singleton
  6. {
  7. private:
  8. Singleton() = default;
  9. ~Singleton() = default;
  10.  
  11. public:
  12.  
  13. Singleton(const Singleton&) = delete;
  14. Singleton& operator=(const Singleton&) = delete;
  15.  
  16. void* operator new(std::size_t) = delete;
  17. void* operator new[](std::size_t) = delete;
  18.  
  19. void operator delete(void*) = delete;
  20. void operator delete[](void*) = delete;
  21.  
  22. static const Singleton& getInstance()
  23. {
  24. static Singleton mySingleton;
  25.  
  26. return mySingleton;
  27. }
  28.  
  29. void foo() const
  30. {
  31. std::cout << this;
  32. }
  33. };
  34.  
  35.  
  36. int main(int argc, const char *argv[])
  37. {
  38. const Singleton& s1 = Singleton::getInstance();
  39.  
  40. // Why does this compile?
  41. std::vector<Singleton> v(2);
  42.  
  43. v.front().foo();
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0x8750008