zpp_bits icon indicating copy to clipboard operation
zpp_bits copied to clipboard

Can the function signature of explicit serialization not use `auto`?

Open fenghou1st opened this issue 1 year ago • 3 comments

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.

fenghou1st avatar Aug 23 '22 21:08 fenghou1st

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

eyalz800 avatar Aug 24 '22 05:08 eyalz800

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++.

fenghou1st avatar Aug 25 '22 00:08 fenghou1st

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

eyalz800 avatar Aug 25 '22 06:08 eyalz800

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.

fenghou1st avatar Aug 25 '22 09:08 fenghou1st