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