#if Q_BYTE_ORDER = Q_LITTLE_ENDIAN apparently not working
-
Following the documentation examples on the use of Q_BYTE_ORDER:
@#if Q_BYTE_ORDER == Q_BIG_ENDIAN
...
#endif@or
@ #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
...
#endif@I successfully tried
@QString endianType;
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
endianType="BIG ENDIAN";
#endif@but when I tried
@QString endianType;
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
endianType="LITTLE ENDIAN";
#endif@I got a compile error:
expected constructor, destructor, or type conversion before "=" tokenYet if I modify the code to
@#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
QString endianType="LITTLE ENDIAN";
#endif@it compiles ok. While I can use this work arround I'm wondering if anyone knows the reason for this compile difference between the use of Q_LITTLE_ENDIAN and Q_BIG_ENDIAN when compared to Q_BYTE_ORDER>
-
You probably have this code at global scope; the compiler may have even said so on the line just before the error message. The statement
endianType="LITTLE ENDIAN";
is invalid at that point.Try the following instead:
@
void foo()
{
QString endianType;
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
endianType="LITTLE ENDIAN";
#endif
}
@There's no error when you use "the workaround" of
QString endianType="LITTLE ENDIAN";
because initialization is permitted at global (or namespace) scope.Finally, there's no error when you have
#if Q_BYTE_ORDER == Q_BIG_ENDIAN
because your machine isn't big-endian so the line that would cause the error never gets compiled. -
Thanks mburr!
I did some more testing and you pinpointed the problem exactly.