I would like to have a base class that I derive from that offers serialization mechanism:
template <typename Type, typename Class, Type Class::* Member>
class Serializable
{
public:
void Serialize();
};
template <typename Type, typename Class, Type Class::* Member>
void Serializable<Type, Class, Member>::Serialize()
{
// accessing the member variable we're pointing to, to do stuff with it
}
The idea is that if I derive Foo
from Serializable
, I could template it so that Foo::x
is used as the template parameter. This way, when I call the base Serialize
, it knows to serialize the x
member of Foo
. So I’d be able to do something like:
class Foo : public Serializable<int, Foo, &Foo::x> // <<-- issue here
{
public:
int x, y;
};
int main()
{
Foo foo;
foo.x = 32;
foo.y = 10;
foo.Serialize();
return 0;
}
I don’t see how to template Foo
so that Serializable
gets to know that Foo::x
is the member to serialize. It seems to work well to have Type
and Class
properly templated, but it complains Foo
is incomplete at this point. It feels so close…
The end goal is that Foo
stays as simple as it is in memory, but the templated Serialize()
function knows x
is the member to read. If I get it to work, then I can use further metaprogramming to have multiple members serialized.
How would I achieve that? (getting the correct pointer to member)
7
#define RETURNS(...)
-> decltype(__VA_ARGS__)
noexcept(noexcept(__VA_ARGS__))
{ return __VA_ARGS__; }
#define MEMBER(...)
[](auto* self)
RETURNS( self->__VA_ARGS__ )
Now, MEMBER(x)(foo)
acts like a member pointer. If you really need a member pointer,
#define MEMPTR(...)
[](auto* self)
RETURNS( &std::decay_t<decltype(*self)>::__VA_ARGS__ )
Now, MEMPTR(x)(foo)
is &Foo::x
.
The trick is both of these do not require the class to be defined prior to using them, as they get their class type later. They just need the member name.
You simply change:
template <class Derived, auto Member>
class Serializable
then either use Member(static_cast<Derived*>(this))
and either extract Type Class::*Member
or just get a direct reference to this->Member
.
The child looks like:
class Foo : public Serializable<Foo, MEMPTR(x)>
and can be easily extended to
class Foo : public Serializable<Foo, MEMPTR(x), MEMPTR(y)>
(or the MEMBER
versiom).
The MEMPTR
one gets you the exact type (if needed), while MEMBER
just gets you access to it (an int&
cannot be distinguished as easily from an int&&
).
2