Skip to content

Instantly share code, notes, and snippets.

@codehz
Last active October 17, 2023 03:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save codehz/a5581336986a612067bc7d4d84a49625 to your computer and use it in GitHub Desktop.
Save codehz/a5581336986a612067bc7d4d84a49625 to your computer and use it in GitHub Desktop.
#include <cstddef>
#include <cstdlib>
#include <utility>
template <typename R, auto getter, auto setter, auto offset>
struct property {
inline R *self() {
return reinterpret_cast<R *>(reinterpret_cast<size_t>(this) -
offset((R *)0));
}
inline operator auto() { return (self()->*getter)(); }
inline void operator=(auto t) { (self()->*setter)(t); }
};
struct X {
private:
int get_a();
void set_a(int);
public:
int padding[25];
[[no_unique_address]] property<
X, &X::get_a, &X::set_a, []<typename T>(T *) { return offsetof(T, a); }>
a;
};
int test(X &x, int val) {
x.a = val;
return x.a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment