问题
I am trying to get rid of violation of Rule 20.5
Sample code:
#define VAL 2
int32_t func(void)
{
int32_t n1 = VAL;
#undef VAL
#define VAL(x) (x*x)
return VAL(n1);
}
Is there any work around for undef here without changing any other lines ?
回答1:
No, there is no work-around. The code is badly written, there is no justification for using the pre-processor like this. It is just obfuscation - get rid of it. Use plain variables instead.
There exists almost no scenario where the use of #undef
is justified. The only valid case I can think of is "X macros", and even those should be used sparsely.
来源:https://stackoverflow.com/questions/47769783/misra-c-2012-rule-20-5-undef-should-not-be-used