fork download
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. template<typename T, typename V>
  6. class Property {
  7. private:
  8. T* ref;
  9. V (*fn)(T);
  10.  
  11. public:
  12. Property(T* val, V (*fn)(T)) {
  13. this->ref = val;
  14. this->fn = fn;
  15. }
  16.  
  17. V operator()() {
  18. return fn(*ref);
  19. }
  20.  
  21. template<typename T2, typename V2>
  22. friend ostream& operator<<(ostream& o, Property<T2, V2> p) {
  23. o << p();
  24. return o;
  25. }
  26.  
  27. };
  28.  
  29. template<typename T, typename V>
  30. void print_stuff(Property<T, V> p) {
  31. cout << p << endl;
  32. }
  33.  
  34. int main() {
  35. int* a = new int();
  36. *a = 25;
  37. Property<int, double> b(a, sqrt);
  38. print_stuff(b);
  39. *a = 36;
  40. print_stuff(b);
  41. delete a;
  42. return 0;
  43. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
5
6