D
Dave Hansen
Please note crosspost.
Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:
typedef void (*State_Fn)(uint8_t);
void error_state(uint8_t status)
{
NOT_USED(status);
/* code handling error but ignoring status */
}
In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was
#define NOT_USED(p) ((void)(p))
In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use
#define NOT_USED(p) ((p)=(p))
He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.
So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?
Thanks,
-=Dave
-=Dave
Often when writing code requiring function pointers, it is necessary
to write functions that ignore their formal parameters. For example,
a state machine function might take a status input, but a certain
error-handling state might ignore it:
typedef void (*State_Fn)(uint8_t);
void error_state(uint8_t status)
{
NOT_USED(status);
/* code handling error but ignoring status */
}
In another group, a poster asked about defining a macro NOT_USED as
shown above to quiet the compiler warning. His suggested
implementation was
#define NOT_USED(p) ((void)(p))
In this particular case, he noted the compiler he was using would
generate the warning even in the presence of this macro. I suggested
he use
#define NOT_USED(p) ((p)=(p))
He was pleased that it worked, but was concerned that the former
implementation was more widely supported, and that the latter might
generate executable code. I for one had never seen the former before,
though I've often seen the latter (usually not hidden behind a macro),
and I've never seen it actually generate code. At least, not in the
last ten years or so.
So I'm curious. Which form (if either) is more common? Are there any
implementations that will generate executable code for the latter?
Thanks,
-=Dave
-=Dave