Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22066

Typedefs with tweaked alignment

$
0
0

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 the aligned attribute?
  • Are there more ways they differ?
  • Is there a standard way, for creating such typedefs (alignment tweaks) for built-in types?

Viewing all articles
Browse latest Browse all 22066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>