constexpr and endianness

New answer (C++20)

c++20 has introduced a new standard library header <bit>.
Among other things it provides a clean, portable way to check the endianness.

Since my old method relies on some questionable techniques, I suggest anyone who uses it to switch to the check provided by the standard library.

Here’s an adapter which allows to use the new way of checking endianness without having to update the code that relies on the interface of my old class:

#include <bit>

class Endian
{
public:
    Endian() = delete;

    static constexpr bool little = std::endian::native == std::endian::little;
    static constexpr bool big = std::endian::native == std::endian::big;
    static constexpr bool middle = !little && !big;
};

Old answer

I was able to write this:

#include <cstdint>

class Endian
{
private:
    static constexpr uint32_t uint32_ = 0x01020304;
    static constexpr uint8_t magic_ = (const uint8_t&)uint32_;
public:
    static constexpr bool little = magic_ == 0x04;
    static constexpr bool middle = magic_ == 0x02;
    static constexpr bool big = magic_ == 0x01;
    static_assert(little || middle || big, "Cannot determine endianness!");
private:
    Endian() = delete;
};

I’ve tested it with g++ and it compiles without warnings. It gives a correct result on x64.
If you have any big-endian or middle-endian proccesor, please, confirm that this works for you in a comment.

Leave a Comment

deneme bonusudeneme bonusu veren sitelerpulibet girişOnwin Güncel Giriştürkçe altyazılı pornocanlı bahis casino