title: C++ alignment structures
body: <p>Its open season on #defines and other macros and I was looking at a portable way of aligning structures 32 bit and 64 bit code. Previously the code had a #define AMD64 and padding was added when this was not the case. </p>
  
  <p>Is this a portable way of doing the same, minus the #if !defined(...) ... #endif</p>
  
  <pre><code>constexpr size_t defaultAlignmentBits = 4u;
  constexpr size_t defaultAlignmentBytes = 16u;
  
  template&lt;size_t padLen&gt;
  struct PadIfNonZero {
      uint8_t pad[padLen];
  };
  template&lt;&gt;
      struct PadIfNonZero&lt;0&gt; {
  };
  
  template&lt;typename T&gt;
  using PadToAlignment = PadIfNonZero&lt;(sizeof(T) % defaultAlignmentBytes == 0 ? 0  : defaultAlignmentBytes - sizeof(T) % defaultAlignmentBytes)&gt;;
  </code></pre>
  
  <p>And how can this be improved?</p>