What's the difference between __attribute_((aligned(x))) and C++11 alignas?

Are _attribute((aligned(x))) and C++11 alignas the same?

1 Like
  1. It’s not an error to use __attribute__((__aligned__(n))) if n < alignof(T), but is for alignas(n) (“The combined effect of all alignment-specifiers in a declaration shall not specify an alignment that is less strict than the alignment that would be required for the entity being declared if all alignment-specifiers appertaining to that entity were omitted.” is the text from the C++17 spec). Note that GCC does not currently enforce this, but Clang does (65685 – Reducing alignment with alignas should be rejected).
  2. You can write alignas(T) as an alias for alignas(alignof(T)), but no equivalent __attribute__((__aligned__(T))) alias exists.

Those are the two I know of, there may be more subtle differences, including where the two can be placed.

1 Like

Thanks for your reply!