简体   繁体   English

编译时检查 C++ 中结构的更改

[英]Compile time check for changes to struct in C++

I have a serializable struct in struct.h that I am serializing in serialize.h using a custom serialization method.我在 struct.h 中有一个serializable的结构,我正在使用自定义序列化方法在 serialize.h 中序列化。 If a change is made to my serializable struct I need to make sure that a corresponding change in made in serialize.h as well.如果对我的serializable结构进行了更改,我需要确保在 serialize.h 中也进行了相应的更改。
Typical method of reflection use some macros for this.典型的反射方法为此使用了一些宏。 But the nature of our struct and the way we massage the data during serialization does not work well for this.但是我们的结构的性质和我们在序列化期间处理数据的方式并不能很好地解决这个问题。

Is there some way to do some static compile time check so if the serializable struct is changed in any way without changing the serialize.h then the build will fail.有没有办法做一些 static 编译时检查,所以如果可serializable结构以任何方式更改而不更改 serialize.h,那么构建将失败。

One way I have thought of but prefer to do it as last resort is to make a copy of definition of serializable struct in serialize.h and use boost::is_same check with error message asking for corresponding change in serialize.h我已经想到但更愿意将其作为最后手段的一种方法是在 serialize.h 中复制可serializable结构的定义并使用boost::is_same检查错误消息,要求在 serialize.h 中进行相应更改

We decided to just do a checksum verifications for the code.我们决定只对代码进行校验和验证。 If the serializable struct is modified then the static_assert will fail and the error message asks to update the code and the checksum.如果可serializable结构被修改,则 static_assert 将失败并且错误消息要求更新代码和校验和。 This was a compromise we made to make code easier to read and at the same time ensure we don't forget updating serialization.这是我们为使代码更易于阅读而做出的妥协,同时确保我们不会忘记更新序列化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM