fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. #define RO_PROP(T, N, G) ro_prop<T> N = ro_prop<T>([this]() { return G; })
  7.  
  8. template <typename T> class ro_prop
  9. {
  10. private:
  11. const function<T()> get;
  12.  
  13. public:
  14. ro_prop(const function<T()> &&get) : get(get) {}
  15. operator T() { return get(); }
  16. };
  17.  
  18. struct rect
  19. {
  20. int width, height;
  21. RO_PROP(int, area, width * height);
  22. };
  23.  
  24. int main()
  25. {
  26. rect a { 2, 3 };
  27. int s = a.area;
  28.  
  29. cout << s << endl;
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5516KB
stdin
Standard input is empty
stdout
6