This is an archive of the discontinued LLVM Phabricator instance.

Implement P0463R1: "Endian just Endian"
ClosedPublic

Authored by mclow.lists on Jul 16 2017, 8:08 PM.

Details

Reviewers
EricWF
Summary

Implement the C++2a feature "A compile time endian-ness detection idiom"
Howard's suggested implementation is:

enum class endian
{
#ifdef _WIN32

		little = 0,
		big    = 1,
		native = little

#else

		little = __ORDER_LITTLE_ENDIAN__,
		big    = __ORDER_BIG_ENDIAN__
		native = __BYTE_ORDER__,

#endif
};

but libc++ has it's own macros that have done the work to detect this.

The other option would be to rip out _LIBCPP_LITTLE_ENDIAN and _LIBCPP_BIG_ENDIAN, which is tempting, but that would entail changes in code that has to compile for earlier C++ language versions.

Diff Detail

Event Timeline

mclow.lists created this revision.Jul 16 2017, 8:08 PM
lebedev.ri added inline comments.
include/type_traits
4740

(Apologies for double commenting, did not notice that it was in phab until after replying)

I'm probably wrong, but if this is a C++2a proposal, shouldn't this

#if _LIBCPP_STD_VER > 14

be

#if _LIBCPP_STD_VER > 17

?

mclow.lists added inline comments.Jul 17 2017, 8:46 AM
include/type_traits
4740

Yes it should. Thanks!

majnemer added inline comments.
test/std/utilities/meta/meta.type.synop/endian.pass.cpp
40–43

This is undefined behavior as-is because you are reading from a union member other than the active union member.

I'd recommend a sequence like:

uint32_t i = 0x01020304;
char c[4];
memcpy(c, &i, 4);

Fixed test and #ifdef guard

mclow.lists marked 3 inline comments as done.Sep 18 2017, 1:57 PM
EricWF requested changes to this revision.Nov 3 2017, 12:41 PM
EricWF added inline comments.
include/type_traits
4746

These macros have been changed to be #ifdef style.

4750

Does this else case ever actually happen?

This revision now requires changes to proceed.Nov 3 2017, 12:41 PM

Update macro checks.

lebedev.ri added inline comments.Jan 23 2018, 1:53 AM
test/std/utilities/meta/meta.type.synop/endian.pass.cpp
10

c++17, not c++1z
At least grep says there are no c++1z left in the libc++ tests.

EricWF accepted this revision.Jan 23 2018, 2:48 PM
This revision is now accepted and ready to land.Jan 23 2018, 2:48 PM
mclow.lists closed this revision.Jan 23 2018, 5:55 PM

Committed as revision 323296