fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. namespace problem {
  5. class storage {
  6. struct data_base {};
  7.  
  8. template <class K>
  9. struct data: data_base {
  10. data(const K& v): value_(v) {}
  11. K value_;
  12. };
  13.  
  14. typedef std::vector<data_base*> container_type;
  15.  
  16. public:
  17. ~storage() {
  18. while(!this->VData.empty()) {
  19. delete this->VData.back();
  20. this->VData.pop_back();
  21. }
  22. }
  23. template <class P>
  24. inline void push(P v) {
  25. this->VData.push_back(new data<P>(v));
  26. }
  27. template <class P>
  28. P &get(int i) {
  29. return static_cast<data<P>*>(this->VData[i])->value_;
  30. }
  31. private:
  32. container_type VData;
  33. };
  34. }
  35.  
  36. int main() {
  37. problem::storage testStorage;
  38. testStorage.push(256);
  39. testStorage.push(3.4);
  40.  
  41. std::cout << testStorage.get<int>(0) << std::endl;
  42. std::cout << testStorage.get<double>(1) << std::endl;
  43. }
Success #stdin #stdout 0.02s 2856KB
stdin
Standard input is empty
stdout
256
3.4