Last active
October 17, 2023 03:14
-
-
Save codehz/a5581336986a612067bc7d4d84a49625 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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