The first case is explicitly prohibited according to the #if
documentation:
An expression cannot use sizeof or a type cast operator.
As for the warning, you can either ignore it (because you know that your code is ok), disable it using #pragma
, or simply deduce the condition from if
:
bool sizeMatch = (sizeof(a) == sizeof(b)); if (!sizeMatch){ printf("sizes don't match\n"); return -1; }
Edit: since disabling the error seems to have attracted some attention, here are a few ways to achieve this using #pragma warning
:
#pragma warning (push) #pragma warning (disable: 4127) if(sizeof(a) != sizeof(b)){ #pragma warning (pop)
Pop, obviously, could be done further down the code. Another option might be:
#pragma warning (disable: 4127) if(sizeof(a) != sizeof(b)){ #pragma warning (default: 4127)
Which will return a warning without pressing or popping up.
In any case, this code looks ugly. IMO, just using bool
to get the result of comparing sizeof
(as my first snippet shows) would be the cleanest solution.
source share