C++: swapping bytes of an uint64_t
Technically this is UB.
Writing a value to one member of a union, then reading from another member (that was not written to) of the union is undefined behavior.
You would be better off just casting to a char* and just reversing it.
swap(std::uint64_t&amp; num)
{
    // I like the use of reinterpret_cast here
    // it makes sure that somebody reading the code notices
    // and takes special care to review what is happening.
    //
    char* first = reinterpret_cast&lt;char*&gt;(&amp;num);
    char* last  = first + sizeof(num);

    // The advantage of reverse here:
    //
    // The standard library can have pre built optimizations
    // that will use hardware specific optimizations of this that
    // can convert it to a single instruction on some hardware.
    //
    std::reverse(first, last);
}


The question becomes are you doing this to support endianness?
If so then your code is only going to work on machines that have the same endianness as the platform you are currently working on.
Normally people want to swap from platform byte order to network byte order (or the reverse). Depending on architecture this is either a swap (as you have above) or a NO-OP or for some weird hardware something completely different.
To support this we have specific family of functions:
htonl()   // host to network long  (32 byte)
htons()   // host to network short (16 byte)

ntohl()   // network to host long  (32 byte)
ntohs()   // network to host short (16 byte)

These functions will do the appropriate conversion for your hardware.