The 'Type Attributes'page for gcc, gives a very interesting example of how to tweak the alignment on type aliases:
typedef int more_aligned_int __attribute__ ((aligned (8)));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In this example, more_aligned_int
has different alignment that int
, which becomes apparent when declaring an array of those guys:
aligned_int ar[3];
outputs
error: alignment of array elements is greater than element size
aligned_int ar[3];
^
The standard C++ alternative, would be alignas
, and while I was amazed to find out that you can actually write:
using aligned_int = int alignas(8);
compiling the above gives:
warning: attribute ignored [-Wattributes]
using aligned_int = int alignas(8);
note: an attribute that appertains to a type-specifier is ignored
so there is no side effect, and that's why the aforementioned array declaration succeeds. Question time:
- So
alignas
isn't equivalent to thealigned
attribute? - Are there more ways they differ?
- Is there a standard way, for creating such typedefs (alignment tweaks) for built-in types?