https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92900

            Bug ID: 92900
           Summary: Cannot use member of packed struct in constexpr
           Product: gcc
           Version: 9.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tdiff at yandex dot ru
  Target Milestone: ---

Following program generates an error for a struct packed with
__attribute__((packed)) but not with #pragma pack(1)

Compiled as 
g++ -O2 -std=c++17 -Wall -Wextra -Wpedantic -Wstrict-aliasing -fstrict-aliasing

Error: the value of 's2' is not usable in a constant expression
   27 |     constexpr auto sz2 = std::size(s2.i);

Web compiler:
https://godbolt.org/z/h-7i4p


#include <iterator>

#pragma pack(1)
struct S1
{
    int i[10];
}
;
#pragma pack()

struct S2
{
    int i[10];
}
__attribute__((packed)) // comment this to make it compile
;

#define LEN(a) (sizeof(a) / sizeof(a[0]));

void f()
{
    S1 s1{};
    constexpr auto sz1 = std::size(s1.i);
    (void)sz1;

    S2 s2{};
    constexpr auto sz2 = std::size(s2.i);
    (void)sz2;

    constexpr auto sz3 = LEN(s2.i);
    (void)sz3;
}

Reply via email to