Note that there are some explanatory texts on larger screens.

plurals
  1. POCross-platform ALIGN(x) macro?
    primarykey
    data
    text
    <p>I would like to create a <code>struct</code> that has a certain alignment.</p> <p>I would like to use the same struct definition for both GCC and VisualC++ compilers.</p> <p>In VisualC++, one typically does this:</p> <pre><code>__declspec(align(32)) struct MyStruct { // ... }; </code></pre> <p>In GCC, one typically does this:</p> <pre><code>struct MyStruct { // ... } __attribute__ ((aligned (32))); </code></pre> <p>I could of course create the appropriate macros to make this work:</p> <pre><code>BEGIN_ALIGNED_STRUCT(32) struct { // ... } END_ALIGNED_STRUCT(32) ; </code></pre> <p>And thus be able to handle both cases transparently, but here I have to duplicate the alignment constant (<code>32</code>), which I'd like to avoid.</p> <p>An alternative in GCC is to put the <code>__attribute__</code> after the struct tag, as mentioned in <a href="http://gcc.gnu.org/onlinedocs/gcc-3.4.0/gcc/Type-Attributes.html" rel="noreferrer">the docs</a>, like so:</p> <pre><code>struct __attribute__ ((aligned (32))) MyStruct { // ... }; </code></pre> <p>And thus I could make this type of syntax work:</p> <pre><code>ALIGNED_STRUCT(32) MyStruct { // ... }; </code></pre> <p>Does anyone have any better versions? Other ideas? I tried a little code searching, but didn't find anything too promising.</p> <hr> <p><strong>Update:</strong> Based on @John's comment, here's another version that could work (I haven't compiled it, but the docs indicate it's an OK idea)</p> <pre><code>struct MyStruct_Unaligned { // ... }; TYPEDEF_ALIGNED(32, MyStruct_Unaligned, MyStruct); // Would expand to one of: // // typedef __declspec(align(32)) MyStruct_Unaligned MyStruct; // // typedef struct __attribute__ ((aligned (32))) MyStruct_Unaligned MyStruct </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload