Can I prevent breaking anti-aliasing rules using this technique?
<p>If I recall correctly, it would be undefined behavior to write to <code>FastKey::key</code> and then read from <code>FastKey::keyValue</code>:</p>

<pre><code>struct Key {
    std::array&lt;uint8_t, 6&gt; MACAddress;
    uint16_t EtherType;
};

union FastKey {
    Key key;
    uint64_t keyValue;
};
</code></pre>

<p>However, I have been told that if I add char array to the union then the UB is cleared:</p>

<pre><code>union FastKey {
    Key key;
    uint64_t keyValue;
    char fixUB[sizeof(Key)];
};
</code></pre>

<p>Is this true?</p>

<h3>Edit</h3>

<p>As usual my understanding was wrong. With the new information I gathered, I think that I can get the key as a <code>uint64_t</code> value like this:</p>

<pre><code>struct Key {
    std::array&lt;uint8_t, 6&gt; MACAddress;
    uint16_t EtherType;
};

union FastKey {
    Key key;
    unsigned char data[sizeof(Key)];
};

inline uint64_t GetKeyValue(FastKey fastKey)
{
    uint64_t key = 0;
    key |= size_t(fastKey.data[0]) &lt;&lt; 56;
    key |= size_t(fastKey.data[1]) &lt;&lt; 48;
    key |= size_t(fastKey.data[2]) &lt;&lt; 40;
    key |= size_t(fastKey.data[3]) &lt;&lt; 32;
    key |= size_t(fastKey.data[4]) &lt;&lt; 24;
    key |= size_t(fastKey.data[5]) &lt;&lt; 16;
    key |= size_t(fastKey.data[6]) &lt;&lt; 8;
    key |= size_t(fastKey.data[7]) &lt;&lt; 0;
    return key;
}
</code></pre>

<p>I suspect that this will be equally fast as the original version. Feel free to correct me.</p>

<h3>Update</h3>

<p>@Steve Jessop I implemented a <a href="http://ideone.com/DlXDo" rel="nofollow">quick benchmark</a> to test the performance of memcpy vs my solution. I'm not a benchmarking expert, so there may be stupid errors in the code the lead to wrong results. However, if the code is right then it would seem that memcpy is much slower.</p>

<p><b>Note: It seems the benchmark is wrong because the time to calculate the time for fast key is always zero.</b> I'll see if I can fix it.</p>
