I had a class like this:
class ByteOrder
{public:
enum class ByteOrderEnum : unsigned char { BIG_ENDIAN = 0, LITTLE_ENDIAN = 1 };
//^^^ error: expected identifier before numeric constant
static inline ByteOrderEnum this_system_byte_order = []() {
return ByteOrderEnum::BIG_ENDIAN; // WHATEVER FOR NOW (JUST EXAMPLE)
}();
}
This error comes up on GCC, but not Visual Studio. And to be honest I think I compiled this also on Clang and it worked fine. So it took me ages to figure out that in my usr/include there is a file named endian.h that's somehow being included in my source. I'm wondering if this is normal, and what it's part of, and if I have to rename my enums.
Here is the offending code in "endian.h":
/* Get the definitions of __*_ENDIAN, __BYTE_ORDER, and __FLOAT_WORD_ORDER. */ #include <bits/endian.h>
#ifdef __USE_MISC
# define LITTLE_ENDIAN __LITTLE_ENDIAN
# define BIG_ENDIAN __BIG_ENDIAN
# define PDP_ENDIAN __PDP_ENDIAN
# define BYTE_ORDER __BYTE_ORDER
#endif
No, if the environment was fully standard-conforming, then neither BIG_ENDIAN nor LITTLE_ENDIAN should be used by the standard library or compiler and you should be free to use these identifiers as you see fit.
However, glibc, the library that you are using to provide the C standard library functionality, also includes a lot of optional functionality that is not included in the standard library and that includes names such as BIG_ENDIAN or LITTLE_ENDIAN (although I couldn't actually find them documented in the glibc man pages).
Normally, when you request the compiler to act standard-conforming with e.g. the option -std=c99, then glibc will default to not include such optional functionality and a user must request their inclusion specifically by defining one of the feature test macros.
However, unfortunately this doesn't work for GCC when compiling C++ on Linux. In this constellation GCC always pre-defines the _GNU_SOURCE feature macro, telling glibc to include GNU-specific functionality that also includes definitions of BIG_ENDIAN and LITTLE_ENDIAN.
This is done because libstdc++, the C++ standard library implementation used with the GCC compiler, doesn't currently work without the GNU extensions. See Why is _GNU_SOURCE defined by default and how to turn it off? for a detailed explanation and authoritative links.
So, to get it to compile on the GCC/glibc/Linux combination, at least if you are going to include any standard library header, you need to use a different identifier. Note that you generally are not allowed to use identifiers for your own purposes that start with an underscore followed by upper-case letter or that contain (anywhere) a double underscore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With