简体   繁体   English

在C ++ 11或更高版本中有没有办法让constexpr确定没有UB的字节序?

[英]In C++11 or later is there a way to have a constexpr that determines endian without UB?

Note that earlier similar questions I found were before C++11 and/or included UB and/or could not be a constexpr. 请注意,我发现的早期类似问题是在C ++ 11之前和/或包含UB和/或不能是constexpr。

Not a dup of Is there a way to do a C++ style compile-time assertion to determine machine's endianness? 不是重复是否有办法做一个C ++风格的编译时断言来确定机器的字节序? or similar 或类似的

AFAIK there is little endian , big endian , and other . AFAIK有little endianbig endian other At a minimum I need to at least not compile if other , even better if other architectures can be added 至少我需要至少不编译other ,如果可以添加other架构则更好

constexpr and endianness was asked earlier and does not include other , which would leave other architectures ill defined as one or the other constexpr和endianness之前被问到并且不包括other ,这会使other架构定义为一个或另一个

Basically I want to be able to specialize a template based on the target architecture's endianness 基本上我希望能够根据目标架构的字节顺序来专门化template

As part of my hash_append work I hope to provide what you're asking for: 作为我的hash_append工作的一部分,我希望提供你所要求的:

https://github.com/HowardHinnant/hash_append/blob/master/endian.h https://github.com/HowardHinnant/hash_append/blob/master/endian.h

other would be detected by: other将被检测到:

endian::native != endian::little && endian::native != endian::big

The first static_assert in this header is currently incorrect with respect to the other issue and should be removed. 此标头中的第一个static_assert目前对于other问题是不正确的,应该删除。

This header is very easy to provide for any given platform. 这个标题很容易为任何给定的平台提供。 But of course it is not portable, and thus is ideal to have it be provided by your std::lib implementor instead. 但当然它不可移植,因此非常适合由std :: lib实现者提供它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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