rttr icon indicating copy to clipboard operation
rttr copied to clipboard

An example using std::function based property getter/setter

Open rinthel opened this issue 6 years ago • 1 comments

Hi, I'm a newbie to RTTR, and decide to reflect my c++ classes using it.

The documentation says that I can use std::function stuffs for setting a property's getter and setter, but I cannot find the example using it. So can you give an working example with std::function based property accessor?

Here's an example which I expect to work:

class Vec {
public:
  float operator [] (size_t index) const { return v[index]; }
  float& operator [] (size_t index) { return v[index]; }
private:
  float v[2];
};

RTTR_REGISTRATION {
  auto getX = [](Vec& v) -> float { return v[0]; };
  auto setX = [](Vec& v, float p) { v[0] = p; };
  auto getY = [](Vec& v) -> float { return v[1]; };
  auto setY = [](Vec& v, float p) { v[1] = p; };
  rttr::registration::class_<Vec>("Vec")
    .property("x", getX, setX)
    .property("y", getY, setY);
}

When I try to compile this example, the compiler reports errors below:

error: static_assert failed "Invalid number of arguments, please provide as first accessor a getter-member-function without arguments.
error: static_assert failed "Invalid number of arguments, please provide as second argument a setter-member-function with exactly one argument."
...

rinthel avatar Dec 12 '18 07:12 rinthel

I've searched through the code and I don't believe this feature is supported. This feature would be very useful, for both std::function and free function setters/getters which operating on an object instance passed by reference as the first argument.

matthew-reid avatar Jul 02 '23 13:07 matthew-reid