constexpr and endianness

后端 未结 8 562
無奈伤痛
無奈伤痛 2020-12-24 12:42

A common question that comes up from time to time in the world of C++ programming is compile-time determination of endianness. Usually this is done with barely portable #if

8条回答
  •  余生分开走
    2020-12-24 13:11

    It is not possible to determine endianness at compile time using constexpr (before C++20). reinterpret_cast is explicitly forbidden by [expr.const]p2, as is iain's suggestion of reading from a non-active member of a union. Casting to a different reference type is also forbidden, as such a cast is interpreted as a reinterpret_cast.

    Update:

    This is now possible in C++20. One way (live):

    #include 
    template
    constexpr bool is_little_endian() {
      for (unsigned bit = 0; bit != sizeof(T) * CHAR_BIT; ++bit) {
        unsigned char data[sizeof(T)] = {};
        // In little-endian, bit i of the raw bytes ...
        data[bit / CHAR_BIT] = 1 << (bit % CHAR_BIT);
        // ... corresponds to bit i of the value.
        if (std::bit_cast(data) != T(1) << bit)
          return false;
      }
      return true;
    }
    static_assert(is_little_endian());
    

    (Note that C++20 guarantees two's complement integers -- with an unspecified bit order -- so we just need to check that every bit of the data maps to the expected place in the integer.)

    But if you have a C++20 standard library, you can also just ask it:

    #include 
    constexpr bool is_little_endian = std::endian::native == std::endian::little;
    

提交回复
热议问题