title: uintptr_t portable alternative
body: <p>I want to perform check for memory alignment of some type <code>T</code>. The straightforward way to do this is</p>
  
  <pre><code>if (((uintptr_t)&amp;var &amp; __alignof(T) - 1) == 0) ...
  </code></pre>
  
  <p>however, <code>uintptr_t</code> is not part of existing C++ standard and it is not supported by some compilers, so I'm looking for a portable alternative way to do this and <code>std::ptrdiff_t</code> looks good for me. <code>std::ptrdiff_t</code> guaranteed to be able to store difference between two pointers, but who says that one of those pointers cannot be null pointer? In that case <code>std::ptrdiff_t</code> has to be at least same size as the pointer itself.</p>
  
  <pre><code>template &lt;typename T&gt; bool is_properly_aligned(const T* const ptr)
  {
      std::ptrdiff_t diff = (ptr - static_cast&lt;T*&gt;(0)) * sizeof(T);
      return ((diff &amp; __alignof(T) - 1) == 0);
  }
  </code></pre>
  
  <p>or like that (to get rid of multiplication by <code>sizeof(T)</code>)</p>
  
  <pre><code>template &lt;typename T&gt; bool is_properly_aligned(const T* const ptr)
  {
      std::ptrdiff_t diff =
          reinterpret_cast&lt;const char*&gt;(ptr) - static_cast&lt;const char*&gt;(0);
      return ((diff &amp; __alignof(T) - 1) == 0);
  }
  </code></pre>
  
  <p>What do you think about such kind of solution? Is it portable enough? I don't see any reasons why this should fail, however I would like to confirm.</p>
  
  <p>Thanks.</p>