const unsigned char* p;
int64_t u = ...; // ??
What's the recommended way to read a 64-bit binary little endian integer from the 8 bytes pointed to by p? On x64 a single machine instruction should do, but on big-endian hardware swaps are needed. How does one do this both optimally and portably?
Carl's solution is good, portable enough but not optimal. This begs the question: why doesn't C/C++ provide a better and standardized way to do this? It's not an uncommon construct.
int64_t
is standard C++11. It does assume the type is available though.int64_t u = *(int64_t *) p; u = le64toh(u);
??