I'm converting some code that formerly wrote structs directly into storage, to use serialization. This is because it is pretty annoying having piles of code to unpack structs that were laid out differently in older versions of the code that have been upgraded.
At this stage I am hand-writing the serialization code, I'm not looking for automated serialization.
Pseudocode:
struct S
{
int x;
char ch;
short d;
struct { char a,b,c; } t;
};
void serialize_S(struct S const *s); // ignore details about serialization buffer for now
Supposing I have written serialize_S
correctly. The problem I am concerned about is that someone may add a field to struct S
without updating the serialization function (and without updating any test suite). We may think the new field is being serialized when it isn't.
My question is: Is there anything I can do to generate a compilation error if a field exists in the struct but is not mentioned in the serialization function?
If necessary, the solution could require extra tags or something in the struct definition -- I am prepared to trust that if people are editing the struct definition and see something like a macro SERIALIZE_ME
on the end of each field, they'll realize to add it to the other fields too (or at least, I'd catch this looking at diffs on code review).