Why can I bind a reference to a member of a struct defined using #pragma pack, but not __attribute__(packed))?

Viewed 34

Let's say I declare two of essentially the same structure, one using #pragma pack(1) and another using __attribute__((packed)), like so:

#pragma pack(push, 1)
struct S
{
  int a, b;
};
#pragma pack(pop)

struct __attribute__((packed)) T
{
  int a, b;
};

As far as I understand these should have equivalent semantics. However, if I do

S s;

std::cin >> s.a >> s.b;

then this compiles just fine, whereas if I do

T t;

std::cin >> t.a >> t.b;

then I get a compiler error

error: cannot bind packed field ‘t.T::a’ to ‘int&’

This error on its own I guess makes sense, even if it's annoying to have to work around on a platform where there is actually no problem with this sort of addressing. But why don't I get the same error with S?

0 Answers
Related