http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48138
Summary: __attribute__((aligned)) should give an error when applied to a typedef or template parameter, at least in C++0x mode. Product: gcc Version: 4.6.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassig...@gcc.gnu.org ReportedBy: jyass...@gcc.gnu.org In the C++0x draft, [dcl.align] says: "An alignment-specifier may be applied to a variable or to a class data member, but it shall not be applied to a bit-field, a function parameter, the formal parameter of a catch clause (15.3), or a variable declared with the register storage class specifier. An alignment-specifier may also be applied to the declaration of a class or enumeration type." This does not allow its use on a typedef or next to a typename template parameter. It might make sense for gcc to support that as an extension, but gcc's current behavior is not what people expect that extension to do: $ cat test.cc #include <iostream> #define ALIGNED(x) __attribute__((aligned(x))) struct Char15 { char x[15]; } ALIGNED(8); template<typename T> void print_type_alignment(const T&) { struct { char c; T t; } s; std::cout << sizeof(T) << ' ' << ((char*)&s.t - (char*)&s.c) << '\n'; } int main() { typedef char unaligned[15]; typedef char aligned[15] ALIGNED(8); unaligned x[10]; aligned y[10]; Char15 c15[10]; std::cout << sizeof(unaligned) << ' ' << sizeof(x) << '\n'; std::cout << sizeof(aligned) << ' ' << sizeof(y) << '\n'; std::cout << sizeof(Char15) << ' ' << sizeof(c15) << '\n'; aligned z ALIGNED(8); print_type_alignment<unaligned ALIGNED(8)>(z); } $ g++-mp-4.6 -std=gnu++0x test.cc && ./a.out 15 150 15 152 16 160 15 1 Note that the alignment on the typedef applies to the final variable, not the defined type, which means that interior members of arrays of the defined type have an unexpected alignment. This has been reported several times before (PR43798, PR47557, PR12742, PR42098), but the core problem seems to be that alignments on typedefs aren't supported. __attribute__((aligned)) on template arguments seems to have no effect at all. $ g++-mp-4.6 -v Using built-in specs. COLLECT_GCC=g++-mp-4.6 COLLECT_LTO_WRAPPER=/opt/local/libexec/gcc/x86_64-apple-darwin10/4.6.0/lto-wrapper Target: x86_64-apple-darwin10 Configured with: ../gcc-4.6-20110305/configure --prefix=/opt/local --build=x86_64-apple-darwin10 --enable-languages=c,c++,objc,obj-c++ --libdir=/opt/local/lib/gcc46 --includedir=/opt/local/include/gcc46 --infodir=/opt/local/share/info --mandir=/opt/local/share/man --datarootdir=/opt/local/share/gcc-4.6 --with-local-prefix=/opt/local --with-system-zlib --disable-nls --program-suffix=-mp-4.6 --with-gxx-include-dir=/opt/local/include/gcc46/c++/ --with-gmp=/opt/local --with-mpfr=/opt/local --with-mpc=/opt/local --enable-stage1-checking --disable-multilib --enable-fully-dynamic-string Thread model: posix gcc version 4.6.0 20110305 (experimental) (GCC)