g++ may fail to compile __packed structures

Craig Rodrigues rodrigc at crodrigues.org
Tue Sep 7 05:25:42 PDT 2004

On Mon, Sep 06, 2004 at 01:24:15PM -0700, Pavlin Radoslavov wrote:
> It appears that the lastest g++ compiler that comes with FreeBSD may
> fail to compile a __packed structure when one of its fields is
> passed by reference. At the end of this email is a sample program
> that demonstrates the problem. The compilation error is:
> pavlin at carp[14] g++34 test.cc
> test.cc: In function `int main()':
> test.cc:22: error: cannot bind packed field `f1.foo::f' to `int&'
> Exit 1
> The problem appears to exist only with the recent g++ compiler that
> comes with FreeBSD:

This change was made recently to gcc:

Apparently in C++, you are not allowed to  have non-const references
to packed fields.  See:

Your testcase will compile if you change your copy_out function to:

copy_out(const int& i)
//    i = 0x11223344;

but your testcase will not be too useful.

You might want to follow up on one of the GCC mailing lists.

Craig Rodrigues        
rodrigc at crodrigues.org

More information about the freebsd-current mailing list