https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84194
Kenman Tsang <kentsangkm at gmail dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |kentsangkm at gmail dot com
--- Comment #1 from Kenman Tsang <kentsangkm at gmail dot com> ---
Hi, here is another example that the GCC cannot pack a struct correctly.
Refers to the ticket 83732
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83732), this is not only related
to the diagnostic message, but how we pack POD.
Because the A1 is not a C++98 pod ( but it is a POD according to C++11 ), B1
should be able to process the pack (and the result should be sizeof(B1) == 10)
Compared with Clang-4.0 or above, clang can pack this structure correctly
>From the ticket 83732, my best guess is gcc pack a struct only if it fulfill
C++98 pod, but not the pod in general
-----
main.cpp:34:8: warning: ignoring packed attribute because of unpacked non-POD
field ‘A1 B1::b’
A1 b;
^
main.cpp:39:1: error: static assertion failed
static_assert(sizeof(B1) == 10, "");
^~~~~~~~~~~~~
-----
#include <iostream>
struct A1
{
A1() = default;
constexpr A1(const int& value) noexcept
: value(value)
{
}
constexpr A1(int&& value) noexcept
: value(std::move(value))
{
}
int value;
};
static_assert(std::is_pod<A1>::value,"Pass");
static_assert(sizeof(A1)==4, "Pass");
struct A2
{
A2() = default;
int value;
};
static_assert(std::is_pod<A2>::value,"Pass");
static_assert(sizeof(A2)==4, "Pass");
struct B1
{
char a;
A1 b;
char c;
int d;
} __attribute__((__packed__));
static_assert(std::is_pod<B1>::value,"Pass");
static_assert(sizeof(B1)==10, "This is failed");
struct B2
{
char a;
A2 b;
char c;
int d;
} __attribute__((__packed__));
static_assert(std::is_pod<B2>::value,"Pass");
static_assert(sizeof(B2)==10, "Pass");
int main()
{
std::cout << sizeof(A1) << std::endl;
std::cout << sizeof(A2) << std::endl;
}