zpp_bits
zpp_bits copied to clipboard
Can the function signature of explicit serialization not use `auto`?
struct person
{
constexpr static auto serialize(auto & archive, person & self)
{
return archive(self.name, self.age);
}
std::string name;
int age{};
};
Will this cause const person & self
not to be matched?
I want to write this way, because it enables the following member variables to be parsed by the IDE, so that I can quickly find that the member variables are written incorrectly, or have been deleted.
You need both overloads for const and non const, but there are many ways to work around what you want to do. One of them is to avoid specifying the members - why don’t you use the zpp::bits::members or just rely on aggregate member detection? In the example you gave you don’t even need the serialize function
You need both overloads for const and non const
Got it, thank you.
why don’t you use the zpp::bits::members or just rely on aggregate member detection?
Some members are cached data that only exists at runtime. If you don't save them, you can make the saved file smaller and take less time.
Some members are pointers or references to other objects. I think It will cause a lot of trouble if you save them.
In classes with no members to exclude, I do use zpp::bits::members
.
By the way, there is a mechanism called annotation or attribute in java and C# that can exclude members that do not want to be serialized. All the user has to do is add a line of text similar to a comment above the corresponding member. But there seems to be no corresponding in C++.
The closest I can think of is to use the mutable keyword and somehow have the serialization detect that the member is mutable, or introduce some kind of wrapper class to members that need skipping and have the serialize be empty
I feel that if we can't implement it gracefully, then we shouldn't implement it. It's best to do it the C++ way.