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

            Bug ID: 87137
           Summary: [8 Regression] Non-virtual member function increases
                    struct size
           Product: gcc
           Version: 8.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: manisandro at gmail dot com
  Target Milestone: ---

With gcc-8.0.0 onwards, specifically commit
ab87ee8f509c0b600102195704105d4d98ec59d9, the following test case fails to
compile using either i686-w64-mingw32-g++ or x86_64-w64-mingw32-g++ (but
compiles normally using the native x86_64-linux-g++):

--- test.cpp ---

template <typename ToCheck, unsigned ExpectedSize, unsigned RealSize =
sizeof(ToCheck)>
void check_size() {
  static_assert(ExpectedSize == RealSize, "Size is off!");
}

struct Foo {
    unsigned u1 : 5;
    unsigned u2 : 5;
    unsigned u3 : 3;
    unsigned u4 : 3;
    unsigned u5 : 4;
    unsigned u6 : 2;
    unsigned u7 : 3;
    unsigned u8 : 2;
    unsigned u9 : 1;

    unsigned u10 : 3;
    // 31 bits
    unsigned u11 : 2;
    unsigned u12 : 2;
    unsigned u13 : 2;

    unsigned u14 : 6;
    unsigned u15 : 7;
    unsigned u16 : 1;
    unsigned u17 : 1;
    unsigned u18 : 1;
    unsigned u19 : 1;
    unsigned u20 : 1;

    bool bar() const { return false; }
private:
    unsigned u21 : 1;
    unsigned u22 : 1;
    unsigned u23 : 1;
    unsigned u24 : 1;
    // 59 bits
};

int main(int argc, char* argv[]) {
    check_size<Foo, 8>();
    return 0;
}

-----

$ x86_64-w64-mingw32-g++ test.cpp -o test.exe
test.cpp: In instantiation of 'void check_size() [with ToCheck = Foo; unsigned
int ExpectedSize = 8; unsigned int RealSize = 12]':
test.cpp:41:24:   required from here
test.cpp:3:30: error: static assertion failed: Size is off!
   static_assert(ExpectedSize == RealSize, "Size is off!");


Notice the presence of the bar member function. Without the member function,
sizeof(Foo) = 8 as expected, but with the member function, sizeof(Foo) = 12.

Reply via email to