fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template < class Count >
  5. struct Counter {
  6. using count_type = Count;
  7. count_type instances = 0;
  8. };
  9.  
  10. template < class Cnt, Cnt& c>
  11. struct Counted {
  12. using id_type = typename Cnt::count_type;
  13. id_type id;
  14. Counted() : id(c.instances) { ++c.instances; }
  15. ~Counted() { --c.instances; }
  16. };
  17.  
  18. template < class Cnt, Cnt& c >
  19. std::ostream& operator<<(std::ostream& os, Counted<Cnt,c> sib) {
  20. os << "printing sibling " << sib.id;
  21. return os;
  22. }
  23.  
  24. using SCounter = Counter<std::size_t>;
  25. SCounter sc;
  26. using SCounted = Counted<SCounter, sc>;
  27.  
  28. int main() {
  29. // your code goes here
  30. SCounted i, j, k; // --> ok, works fine
  31. std::cout << j << std::endl;
  32. return 0;
  33. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
printing sibling 1