One can assign an ASCII literal (can't call it a string) to enum
value as following:
#include <iostream>
// Macro to handle BIG/LITTLE ENDIAN
// Endianness is suppoesed to handled in this macro
#define TEMP(X) X
enum t
{
XX = 'AA', // 0x4141 or 0100 0001 0100 0001
};
int main()
{
std::cout<<XX<<std::endl;
}
And compiler compiles it and generates a Hexa-decimal constant at compile time, 0x4141
in this case. It does generate a compilation warning as:
main.cpp:9:14: warning: multi-character character constant [-Wmultichar]
XX = 'AA', // 0x4141 or 0100 0001 0100 0001
My question here is, can we avoid this warning?
Or can we write a more elegant code to achieve similar result, probably using templates and constexpr?
I am looking for a portable alternative, so that I can get rid of this as part of refactoring without affecting the core logic.
0x4141
? – DevSolar Aug 25 '16 at 11:58-Wno-multichar
. Be aware that you're relying on implementation defined behaviour though. So, this doesn't help you, but might help other readers. – Sander De Dycker Aug 25 '16 at 11:59XX = 'AA', // 0x4141
- There is no requirement to use ASCII encoding, it can be0x41410000
or some other combinations. This is an XY-problem. Why do you want to use character-enums in the first place? – too honest for this site Aug 25 '16 at 12:11