How to get values from unaligned memory in a standard way?
<p>I know C++11 has some standard facilities which would allow to get integral values from unaligned memory. How could something like this be written in a more standard way?</p>

<pre><code>template &lt;class R&gt;
inline R get_unaligned_le(const unsigned char p[], const std::size_t s) {
    R r = 0;
    for (std::size_t i = 0; i &lt; s; i++)
        r |= (*p++ &amp; 0xff) &lt;&lt; (i * 8); // take the first 8-bits of the char
    return r;
}
</code></pre>

<p>To take the values stored in litte-endian order, you can then write:</p>

<pre><code>uint_least16_t value1 = get_unaligned_le&lt;uint_least16_t &gt; (&amp;buffer[0], 2);
uint_least32_t value2 = get_unaligned_le&lt;uint_least32_t &gt; (&amp;buffer[2], 4);
</code></pre>
