fork(1) download
  1. #include <iostream>
  2. #include <map>
  3. using namespace std;
  4.  
  5. template<typename T>
  6. struct ValueProxy
  7. {
  8. ValueProxy(T& v)
  9. : value(v)
  10. {
  11. }
  12.  
  13. operator T&()
  14. {
  15. return value;
  16. }
  17.  
  18. operator const T&() const
  19. {
  20. return value;
  21. }
  22.  
  23. ValueProxy& operator=(const T& nv)
  24. {
  25. value = nv;
  26. return *this;
  27. }
  28.  
  29. T& value;
  30. };
  31.  
  32. template<typename Key, typename Value>
  33. struct SomeMap
  34. {
  35. // Helpers
  36. using parent_t = std::map<Key, Value>;
  37. parent_t holder;
  38.  
  39. // Part of interest
  40. ValueProxy<Value> operator[](const Key& key)
  41. {
  42. return {holder[key]};
  43. }
  44. };
  45.  
  46. void modify(int& a) {// базовый метод
  47. a += 5;
  48. }
  49.  
  50. void dump(const SomeMap<int,int> &mp)
  51. {
  52. for (auto v : mp.holder)
  53. {
  54. cout << std::get<1>(v) << ' ';
  55. }
  56. cout << endl;
  57. }
  58.  
  59. int main()
  60. {
  61. SomeMap<int, int> mp;
  62.  
  63. // Proxy test 1
  64. mp[2] = 100;
  65. dump(mp);
  66.  
  67. // Proxy test 2
  68. modify(mp[2]);
  69. dump(mp);
  70.  
  71. // Proxt test 3
  72. mp[2] = mp[2] + 100;
  73. dump(mp);
  74.  
  75.  
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
100 
105 
205