Skip to content

Instantly share code, notes, and snippets.

@MehdiNS
Created July 5, 2017 01:19
Show Gist options
  • Save MehdiNS/b2e29aabe3fb375b016a4a32c41ad849 to your computer and use it in GitHub Desktop.
Save MehdiNS/b2e29aabe3fb375b016a4a32c41ad849 to your computer and use it in GitHub Desktop.
Endianness at compile-time
// """"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