Created
July 5, 2017 01:19
-
-
Save MehdiNS/b2e29aabe3fb375b016a4a32c41ad849 to your computer and use it in GitHub Desktop.
Endianness at compile-time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// """"Should"""" work with any C+14 compiler | |
// (And yes, there is a beautiful UB in this code) | |
#include <iostream> | |
struct Endianness | |
{ | |
constexpr static bool isBig() | |
{ | |
union | |
{ | |
unsigned int i = 0x01000000; | |
char c[4]; | |
}; | |
return c[0]; | |
} | |
}; | |
int main() | |
{ | |
std::cout << Endianness::isBig() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment