0

With the relaxation of constraints on constant expression, is there a way in C++14 to obtain the system endianness at compile-time ?

constexpr bool is_big_endian()
{
    // Something here
}

constexpr bool is_little_endian()
{
    // Something here
}
Vincent
  • 57,703
  • 61
  • 205
  • 388
  • 1
    I haven't heard of a constant for that but you can get the common #define the compiler will give you about the architecture. If you're going to make it work on different architectures you should know their endianness so you can easily use some #ifdef – meneldal Jun 21 '14 at 13:24
  • 1
    A `reinterpret_cast` is still not allowed in constant expressions; and you can still write "endian-independent" code with shift-operators. – dyp Jun 21 '14 at 13:32
  • 1
    http://stackoverflow.com/questions/4239993/determining-endianness-at-compile-time – phuclv Jun 21 '14 at 13:33
  • 1
    other ways: http://esr.ibiblio.org/?p=5095 https://gcc.gnu.org/ml/gcc-help/2007-07/msg00342.html or the [predefined macros](https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html) `__ORDER_LITTLE_ENDIAN__`, `__ORDER_BIG_ENDIAN__` in gcc – phuclv Jun 21 '14 at 13:37
  • How about without boost? does a `union` help of `reinterpret_cast` is not allowed? – towi Jun 26 '14 at 12:47
  • using a `union` http://stackoverflow.com/a/24431286/472245 – towi Jun 26 '14 at 13:02

0 Answers0