https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109824
Bug ID: 109824 Summary: aligned attribute lost on first usage Product: gcc Version: 12.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: stsp at users dot sourceforge.net Target Milestone: --- Created attachment 55063 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55063&action=edit test case struct a { short aa; }; typedef struct a less_aligned_a __attribute__ ((aligned (1))); static inline void foo(void) { #ifndef BUG struct a aa __attribute__((unused)); #endif } class t1 { less_aligned_a i; public: less_aligned_a &get_ref() { return i; } } __attribute__((packed)); t1 ap; less_aligned_a* a = &ap.get_ref(); ----- $ g++ -Wall -c a.cpp a.cpp: In instantiation of ‘less_aligned_a& t1<T>::get_ref() [with T = int; less_aligned_a = a]’: a.cpp:23:32: required from here a.cpp:18:16: error: cannot bind packed field ‘((t1<int>*)this)->t1<int>::i’ to ‘less_aligned_a&’ {aka ‘a&’} 18 | return i; But comment out the line 9 under "ifndef BUG" and it compiles without an error.