Inheritance and attribute

Consider this code in C ++

struct Base
{
   std::int64_t x;
   std::int64_t y;
};
static_assert(sizeof(Base) == 16, "Base not of size 16!");

struct Derived : Base
{
   std::int32_t z;
}__attribute__((packed));
static_assert(sizeof(Derived) == 20, "Derived not of size 20!");

clang considers this code valid when gcc causes the second static_assert to run. ("It is not made from size 20!). If I add an attribute packed into the database, then this is fine in compilers as well. Does anyone know which one is right and why?

+4
source share
2 answers

Both are correct.

( ), , , , static_assert -, ( ) - , .

__attribute__((packed)) (-), , , , . , , (/ ). , .

+2

, .

, , , , , . , , . , .

+1

Source: https://habr.com/ru/post/1653497/


All Articles