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. template <Counter& c>
  10. struct Counted {
  11. using id_type = count_type;
  12. id_type id;
  13. Counted() : id(c.instances) { ++c.instances; }
  14. ~Counted() { --c.instances; }
  15.  
  16. friend std::ostream& operator<<(std::ostream& os, const Counted& lol) {
  17. os << "printing inner " << lol.id;
  18. return os;
  19. }
  20. };
  21. };
  22.  
  23. using SCounter = Counter<std::size_t>;
  24. SCounter sc;
  25. using SCounted = SCounter::Counted<sc>;
  26.  
  27. int main() {
  28. // your code goes here
  29. SCounted i, j, k; // --> ok, works fine
  30. std::cout << j << std::endl;
  31. return 0;
  32. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
printing inner 1