fork(8) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct X{
  5.  
  6. static std::vector<X*>& getRegistry(){
  7. static std::vector<X*> registry;
  8. return registry;
  9. }
  10.  
  11. X(){
  12. getRegistry().push_back(this); // Each X adds itself to the registry
  13. }
  14. };
  15.  
  16. template<typename T>
  17. struct Y : X{
  18. private:
  19. static T instance; // The per-type singleton
  20. };
  21.  
  22. template<typename T>
  23. T Y<T>::instance {};
  24.  
  25. struct A : Y<A>{};
  26. struct B : Y<B>{};
  27. struct C : Y<C>{};
  28.  
  29. int main(){
  30. std::cout << "Number of objects in the registry: " << X::getRegistry().size() << std::endl;
  31. }
  32.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Number of objects in the registry: 0