fork download
  1. #include <cstddef>
  2.  
  3. template <class Src, class Dest> Dest* transfer_cv(Src *, Dest *p) { return p; }
  4. template <class Src, class Dest> const Dest* transfer_cv(const Src *, Dest *p) { return p; }
  5. template <class Src, class Dest> volatile Dest* transfer_cv(volatile Src *, Dest *p) { return p; }
  6. template <class Src, class Dest> const volatile Dest* transfer_cv(const volatile Src *, Dest *p) { return p; }
  7.  
  8. #define property_enable(type) typedef type property_container_type
  9. #define property_this transfer_cv( this, container_from_property(this) )
  10.  
  11. template <class Value>
  12. struct property_base
  13. {
  14. typedef Value value_type;
  15. value_type value;
  16. };
  17.  
  18. #define property struct : property_base
  19. #define property_set(...) void operator =(__VA_ARGS__)
  20. #define property_get(...) operator __VA_ARGS__()
  21. #define property_make(name) name; \
  22. static property_container_type* container_from_property(const volatile decltype(name)* p) \
  23. { \
  24.   return (property_container_type*) ((char*) p - offsetof(property_container_type, name)); \
  25. }
  26.  
  27. #include <iostream>
  28.  
  29. struct asdf {
  30. property_enable(asdf);
  31.  
  32. float myFloat;
  33.  
  34. property<int>
  35. {
  36. property_set(int new_value) {
  37. value = new_value * 2;
  38. std::cout << "my float:" << property_this->myFloat << std::endl;
  39. }
  40. property_get(int) const {
  41. return value - 1;
  42. }
  43. } property_make(member)
  44. };
  45.  
  46. int main()
  47. {
  48. asdf x = { 2.0f };
  49. x.member = 5;
  50. std::cout << x.member;
  51. }
  52.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
my float:2
9