mardi 26 avril 2022

Automatic Compare Layout of 2 Structs in C++

2 structs came from different Header-Files. How can I ensure the memory layout of them are binary compatibility? In addition how can I check the naming of them are equal? The checking should be done at compile time.

As an Example:

namespace LibA {
  struct Item {
    uint32_t A;
    uint8_t B;
    uint8_t pad1,pad2,pad3;
  };
}


namespace LibB {
  struct Item {
    uint32_t A;
    uint8_t B;
    uint8_t pad1,pad2,pad3;
  };
}

Ok - in the above Example it's easy to do it the manual way with sizeof(...) and offsetof(...).

But the question is to do it in an automatic way like static_assert(issame(LibA::Item,LibB::Item),"Check")?

Aucun commentaire:

Enregistrer un commentaire