3

Note that earlier similar questions I found were before C++11 and/or included UB and/or could not be a constexpr.

Not a dup of Is there a way to do a C++ style compile-time assertion to determine machine's endianness? or similar

AFAIK there is little endian, big endian, and other. At a minimum I need to at least not compile if other, even better if other architectures can be added

constexpr and endianness was asked earlier and does not include other, which would leave other architectures ill defined as one or the other

Basically I want to be able to specialize a template based on the target architecture's endianness

Community
  • 1
  • 1
Glenn Teitelbaum
  • 10,108
  • 3
  • 36
  • 80
  • By 'other' do you mean something exotic like middle-endian or mixed-endian or runtime switchable architectures? – mattnewport Aug 25 '15 at 18:40
  • 1
    as commented below, `__ORDER_PDP_ENDIAN__` would qualify – Glenn Teitelbaum Aug 25 '15 at 18:46
  • 2
    There are architectures where there is no compile time answer to the question since it is a runtime setting. Apparently some PowerPC processors can even select on a per-page basis. – mattnewport Aug 25 '15 at 19:44
  • @mattnewport: A C++ compiler typically targets a hardware/OS combination. Are you aware of such a combination where the endian can vary at C++ application run time? – Howard Hinnant Aug 27 '15 at 14:10
  • @HowardHinnant I'm only going by Wikipedia: https://en.wikipedia.org/wiki/Endianness#Bi-endian_hardware - I haven't actually encountered such a system myself in the wild. It sounds like they exist though, perhaps in embedded systems. – mattnewport Aug 27 '15 at 14:43

1 Answers1

5

As part of my hash_append work I hope to provide what you're asking for:

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

other would be detected by:

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.

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.

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577
  • I was hoping for something that works on all compliant compilers on all architectures. BTW, for GNU there seems to also be `__ORDER_PDP_ENDIAN__` – Glenn Teitelbaum Aug 25 '15 at 18:09
  • Yeah, me too. Bar bet: Name a machine for which `__BYTE_ORDER__ == __ORDER_PDP_ENDIAN__`, and there exists a compiler which defines `__cplusplus >= 201103`. :-) – Howard Hinnant Aug 25 '15 at 20:27
  • We don't make bar bets on SO, we make posts :) :http://stackoverflow.com/questions/32218129/what-exactly-is-order-pdp-endian – Glenn Teitelbaum Aug 26 '15 at 12:26
  • Well done! :-) You neglected the second part of that bet ... er post: `__cplusplus >= 201103`. – Howard Hinnant Aug 26 '15 at 14:19
  • First things first. I was hoping that might be in somebodt's answer. But, given the lack of activity, I doubt I'll get to part two. At least now `__ORDER_PDP_ENDIAN__` is searchable on SO – Glenn Teitelbaum Aug 27 '15 at 14:21