fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <utility>
  4.  
  5. class Cache
  6. {
  7. public:
  8. struct Value
  9. {
  10. int value = 1;
  11. int get() { return value; }
  12. void set(int newvalue) { value = newvalue; }
  13. };
  14.  
  15. Value& For(int a, int b) {
  16. return values[std::make_pair(a, b)];
  17. }
  18.  
  19. friend std::ostream& operator<<(std::ostream &os, const Cache &c)
  20. {
  21. for(auto &value : c.values)
  22. std::cout << '[' << value.first.first << ',' << value.first.second << "] = " << value.second.value << std::endl;
  23. return os;
  24. }
  25.  
  26. private:
  27. std::map<std::pair<int,int>, Value> values;
  28. };
  29.  
  30. class Flight
  31. {
  32. public:
  33. class Proxy
  34. {
  35. private:
  36. Flight &f;
  37. int a, b;
  38.  
  39. public:
  40. Proxy(Flight &f, int a, int b) : f(f), a(a), b(b) {}
  41.  
  42. operator int() {
  43. return f.cache.For(a, b).get();
  44. };
  45.  
  46. Proxy& operator=(int value) {
  47. f.cache.For(a, b).set(value);
  48. return *this;
  49. }
  50. };
  51.  
  52. friend class Proxy;
  53.  
  54. Proxy operator()(int a, int b) {
  55. return Proxy{*this, a, b};
  56. }
  57.  
  58. friend std::ostream& operator<<(std::ostream &os, const Flight &f)
  59. {
  60. return os << f.cache;
  61. }
  62.  
  63. private:
  64. Cache cache;
  65. };
  66.  
  67. int main()
  68. {
  69. Flight s;
  70. int value = s(1, 2);
  71. s(3, 4) = value;
  72. std::cout << s;
  73. return 0;
  74. }
Success #stdin #stdout 0s 4404KB
stdin
Standard input is empty
stdout
[1,2] = 1
[3,4] = 1