2

I'm writing some network code in C++11, and I have a doubt about the size of some packets.

Is the size of the following two structures guaranteed by the standard to be the same? even without attribute((packed)); (that I know it's non standard).

struct A
{
uint16_t a;
} __attribute__((packed));

template<class T>
struct B : A
{
T getA() { return static_cast<T>(a);}
} __attribute__((packed));

Thanks!

Saturnu
  • 309
  • 1
  • 8
  • 1
    Why do you wonder? What is the *real* problem you need to solve? – Some programmer dude Apr 29 '19 at 07:50
  • 4
    you can `static_assert` that the sizes are the same so the compiler will yell if it doesn't meet your expectations – kmdreko Apr 29 '19 at 07:57
  • 1
    [the answers here](https://stackoverflow.com/questions/49407290/memory-layout-differences-in-structs) imply there is some guarantee, though `packed` may weaken or strengthen that – kmdreko Apr 29 '19 at 07:58
  • @Someprogrammerdude two things: (1) I just want to know this for personal knowledge (2) the real problem is to write this code in a library and use other types (enums for instance) in the client code instead of uint16_t in this case. – Saturnu Apr 29 '19 at 07:59
  • @kmdreko yes, that was something I wanted to do, but I preferred to be sure anyway – Saturnu Apr 29 '19 at 08:00

0 Answers0