I have a set of structs which come from some C code I’m converting to C++, they should be POD types according to what I understood. Here are some examples:
struct Data {
u16 type : 12;
u8 variant : 3;
bool isTop : 1;
};
struct DData {
u16 type : 12;
u8 variant : 3;
u8 layer : 1;
};
struct TData {
struct Data data1, data2;
struct DData ddata[MAX];
u16 x;
u8 y;
u8 s : 4;
u8 l : 4;
u8 wl : 3;
u8 wr : 3;
u8 lt : 2;
};
I have many methods which up to now had pointer to these structs as parameters and I was planning to move them directly inside the struct declarations to save a lot of typing, eg.
struct Data {
u16 type : 12;
u8 variant : 3;
bool isTop : 1;
inline bool hasFlag(u64 flag) { return Types::specs[type].flags & flag; }
};
I was wondering if every method I can add in this way will be safe for this struct, these structures are serialized over the network and into binary files so I need to be sure that this can’t potentially break anything. If I’m right they should remain POD types so no particular problems or overhead should appear.
That should remain a POD.
You can be sure by using the
std::is_podtrait:will be
trueifDatais a POD, false otherwise.Anyway, I wouldn’t bother moving the functions into the struct body. You’re not really gaining anything besides a different calling syntax.