I am aware that testing __cplusplus
via preprocessor is possible, however compilers phase in C++20 support in various stages so this is not a reliable indicator in either direction.
Indeed! In fact, this is why we now have an entirely new method of detecting feature support for compilers that are in various stages of release: feature-test macros! If you look at SD-FeatureTest, there are large tables of macros corresponding to each language and library feature adopted into the standard, where each macro will be defined with the specified value once a feature is adopted.
In your specific case, you want __cpp_lib_endian
.
Now the problem with library macros is that we started added them before we added a place to put them - which would be <version>
. If you're on a compiler that has <version>
(which would be gcc 9+, clang 7+ with libc++, and I don't know about MSVC), you can just include that. But it might not, so you might also have to try to include the correct header (in this case <bit>
).
#if __has_include(<bit>)
# include <bit>
# ifdef __cpp_lib_endian
# define HAS_ENDIAN 1
# endif
#endif
#ifdef HAS_ENDIAN
constexpr bool is_little_endian = std::endian::native == std::endian::little;
#else
constexpr bool is_little_endian = /* ... figure it out ... */;
#endif
Although it might be better to do:
#ifdef HAS_ENDIAN
using my_endian = std::endian;
#else
enum class my_endian {
// copy some existing implementation
};
#endif
constexpr bool is_little_endian = my_endian::native == my_endian::little;
-std=c++2a
does not havestd::endian
" godbolt.org/z/aRFCps – Melisandrastd::endian
and then conditionally defining a preprocessor symbol, but I have a feeling that's not the answer you're looking for. – Pulvinateusing namespace std;
will allow unqualifiedendian
to findstd::endian
if that exists. If not,endian
is looked up in the surrounding namespace. – Ogleconstexpr
andruntime_detection()
on the same line ;-) – Pneumoconiosis