fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. struct VectorCache
  6. {
  7. VectorCache() : instance_counter_(++s_instance_counter_) { }
  8.  
  9. template<typename T>
  10. std::vector<T>& GetTs()
  11. {
  12. static std::vector<std::vector<T>> tss;
  13. if (tss.size() <= instance_counter_)
  14. tss.resize(instance_counter_);
  15. return tss[instance_counter_];
  16. }
  17.  
  18. size_t instance_counter_;
  19. static size_t s_instance_counter_;
  20. };
  21. size_t VectorCache::s_instance_counter_;
  22.  
  23. int main()
  24. {
  25. VectorCache vc1, vc2;
  26. std::vector<int>* pi1 = &vc1.GetTs<int>();
  27. std::vector<float>* pf1 = &vc1.GetTs<float>();
  28. std::vector<int>* pi2 = &vc2.GetTs<int>();
  29. std::vector<float>* pf2 = &vc2.GetTs<float>();
  30. std::cout << pi1 << ' ' << pf1 << ' ' << pi2 << ' ' << pf2 << '\n';
  31. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
0x87fd014 0x87fd024 0x87fd040 0x87fd060