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

            Bug ID: 109654
           Summary: unnecessary "cannot bind packed field to reference"
                    error when referenced type has aligned(1) attribute
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: richard-gccbugzilla at metafoo dot co.uk
  Target Milestone: ---

As a workaround for people hitting #36566, I think GCC should accept cases like
this:


typedef __attribute__((aligned(1))) int packed_int;

struct __attribute__((packed)) Foo {
    int i;
    packed_int& get() { return i; }
};


Unfortunately GCC rejects:


<source>:5:32: error: cannot bind packed field '((Foo*)this)->Foo::i' to
'packed_int&' {aka 'int&'}
    5 |     packed_int& get() { return i; }
      |    


And conversely, GCC accepts this code, which has a genuine misalignment issue:


typedef __attribute__((aligned(1))) int packed_int;

struct __attribute__((packed)) Foo {
    packed_int i;
    int& get() { return i; }
};


I wonder if the check is mistakenly looking at the alignment of the source type
instead of the alignment of the referent of the reference type?
  • [Bug c++/109654] New... richard-gccbugzilla at metafoo dot co.uk via Gcc-bugs

Reply via email to