fork download
  1. #include <iostream>
  2. #include <unordered_set>
  3. #include <memory>
  4. #include <vector>
  5. #include <deque>
  6.  
  7. template < typename T, typename TAG = void > struct magic
  8. {
  9. // ...
  10. /* private: */ static std::unordered_set<T*> objects ;
  11. };
  12.  
  13. template < typename T, typename TAG > std::unordered_set<T*> magic<T,TAG>::objects ;
  14.  
  15. template < typename T, typename TAG = void > struct tracked
  16. : T, magic< tracked<T,TAG>, TAG >
  17. {
  18. template < typename ... ARGS > tracked( ARGS... args ) : T(args...) {}
  19. };
  20.  
  21. struct A { /* ... */ };
  22.  
  23. template < int N > struct use_case {} ;
  24.  
  25. int main()
  26. {
  27. // the general solution is not imposed; nothing needs to be done to
  28. // 'let the specific use-cases choose how to store the objects'
  29. std::vector< std::shared_ptr<A> > seq1(5) ;
  30. for( int i = 0 ; i < 5 ; ++i ) seq1.emplace_back( new A ) ;
  31.  
  32. // the general solution can be used in conjunction with aspecial case solution
  33. // here, objects participate multiple use cases
  34. // all objects participate in the this use case, half of them also participate
  35. // in the first use case, and the other half also participates in
  36. // a new use-case using the general solution.
  37. std::deque< std::shared_ptr<A> > seq2 { seq1.begin(), seq1.end() } ;
  38. for( int i = 0 ; i < 5 ; ++i ) seq2.emplace_front( new tracked< A, use_case<1> > ) ;
  39.  
  40. // here, the same set of objects participate in two different use cases
  41. // with both use cases implemented using just the general solution
  42. tracked< A, use_case<2> > three[5] ;
  43. std::vector< std::reference_wrapper<A> > four ;
  44. for( A a : three )
  45. four.emplace_back( tracked< std::reference_wrapper<A>, use_case<3> >(a) ) ;
  46. }
  47.  
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
Standard output is empty