简体   繁体   中英

Testing endianess at compile-time: is this constexpr function correct according to the standard?

After some search for a way to check endianess at compile-time I've come up with the following solution:

static const int a{1};

constexpr bool is_big_endian()
{
    return *((char*)&(a)) == 1;
}

GCC accepts this code only in some contexts where constexpr is required:

int b[is_big_endian() ? 12 : 25]; //works
std::array<int, testendian() ? 12 : 25> c;  //fails

For the second case, GCC says error: accessing value of 'a' through a 'char' glvalue in a constant expression . I couldn't find anything in the standard that forbids such thing. Maybe someone could clarify in which case GCC is correct?

This is what I get from Clang 3.1 ToT:

error: constexpr function never produces a constant expression

§5.19 [expr.const]

p1 Certain contexts require expressions that satisfy additional requirements as detailed in this sub-clause; other contexts have different semantics depending on whether or not an expression satisfies these requirements. Expressions that satisfy these requirements are called constant expressions .

p2 A conditional-expression is a core constant expression unless it involves one of the following as a potentially evaluated subexpression:

  • [...]
  • a reinterpret_cast (5.2.10);

So, (char*)&(a) evaluates to a reinterpret_cast , as such the function is never a valid constexpr function.

You should look into Boost.Detail.Endian

It is a mapping of several architectures to their endianness (through the macros BOOST_BIG_ENDIAN, BOOST_LITTLE_ENDIAN, and BOOST_PDP_ENDIAN). As far as I know, there is no actual way to determine the endianness at compile time, other than a list like this.

For an example implementation that uses Boost.Detail.Endian, you can see the library I'm hoping to get reviewed for submission to Boost: https://bitbucket.org/davidstone/endian/ (the relevant file is byte_order.hpp , but unsigned.hpp is necessary as well if you want to just use my implementation).

如果实现N3620 - 网络字节顺序转换,您将能够使用constexpr ntoh检查字节顺序,但请记住,有一些罕见的体系结构,如中端,您永远无法支持所有这些。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM