Skip to main content

Timeline for Checking endianness at compile-time

Current License: CC BY-SA 3.0

8 events
when toggle format what by license comment
Jul 1, 2016 at 10:27 comment added barak manos @FaTony: I have pretty much mentioned that in you will have to... make sure that it is placed in a memory address aligned to 4 bytes (and that by itself might yield some platform-dependency issues...).
Jul 1, 2016 at 8:44 comment added user109835 -1, this violates strict aliasing rules.
Mar 29, 2014 at 19:17 comment added barak manos @icdae: Please note the emphasis on the word Alternatively, which means that you should leave endianValues[4] = {0, 1, 2, 3} as is, but read it using *(uint32_t*)endianValues). The value variable is meant to be used only in the first option suggested.
Mar 29, 2014 at 18:37 comment added icdae Oops, I misspoke (just woke up, everything's a bit fuzzy); being a pointer to a constant value won't change how the value itself is stored in memory. The function will always return the same result since value = HL_LITTLE_ENDIAN. As for the switch statement, one of the restrictions for a constexpr statement is that the body of the function can only consist of a single return statement. In this case the switch can be converted to a simple series of ternary operators.
Mar 29, 2014 at 18:21 comment added barak manos @icdae: It's not an 8-bit pointer, it's a 32-bit pointer (look inside the switch statement that you removed). Though I have to admit, I'm not really sure how it is applied into a constexpr during compile-time.
Mar 29, 2014 at 18:17 comment added icdae I tried your solution (had to remove the switch statement to make it a constexpr function) but I believe this will always return as little endian. Since the value stored in HL_LITTLE_ENDIAN is how it will actually be stored in memory on all computers, retrieving an 8-bit pointer to value is always going to grab the same value of 0x03. I tried another solution though end edited it into the original question.
Mar 29, 2014 at 17:59 history edited barak manos CC BY-SA 3.0
added 724 characters in body
Mar 29, 2014 at 17:42 history answered barak manos CC BY-SA 3.0