Portable Unused Parameter MACro Used on Function Signature for C and C++

Portable UNUSED parameter macro used on function signature for C and C++

After testing and following the comments, the original version mentioned in the question turned out to be good enough.

Using: #define UNUSED(x) __pragma(warning(suppress:4100)) x (mentioned in comments), might be necessary for compiling C on MSVC, but that's such a weird combination, that I didn't include it in the end.

How can I suppress unused parameter warnings in C?

I usually write a macro like this:

#define UNUSED(x) (void)(x)

You can use this macro for all your unused parameters. (Note that this works on any compiler.)

For example:

void f(int x) {
UNUSED(x);
...
}

defining unused parameters in C

You can cast the parameter to void like this:

void *timer1_function(void * parameter1) {
(void) parameter1; // Suppress the warning.
// <statement>
}

Universally compiler independent way of implementing an UNUSED macro in C/C++

According to this answer by user GMan the typical way is to cast to void:

#define UNUSED(x) (void)(x)

but if x is marked as volatile that would enforce reading from the variable and thus have a side effect and so the actual way to almost guarantee a no-op and suppress the compiler warning is the following:

// use expression as sub-expression,
// then make type of full expression int, discard result
#define UNUSED(x) (void)(sizeof((x), 0))

How to supress unused (void **arg) parameter?

Use the parameter in an expression casted to void. Then the parameter is "used".

bool decodeString(pb_istream_t *stream, const pb_field_t *field, void **arg)
{
(void)arg;
...
}

How to elegantly fix this unused variable warning?

#ifdef DEBUG
#define CHECK(x) x
#else
#define CHECK(x) ((void)sizeof((void)(x),0))
#endif

I think this addresses all of the possible issues :

  • sizeof ensures that the expression is not evaluated at all, so its side-effects don't happen. That is to be consistent with the usual behaviour of debug-only constructs, such as assert.
  • ((x), 0) uses the comma operator to swallow the actual type of (x). This is to prevent VLAs from triggering evaluation.
  • (void) explicitly ignores the result of (x) and sizeof so no "unused value" warning appears.

How to make sure that a variable is not used

C++ allows you to have unnamed function parameters. It's exactly useful for situations where you do not intend on using the corresponding parameter, yet must declare it to match the signature e.g. when writing a callback to some library.

Simply redefine your function:

void f(int&) {
// code ...
}

Function pointer cast as void without being called

It doesn't do anything, except one thing. Stopping the compiler from complaining about unused variables. It is actually being recommended here, on SO, as a portable way of doing it: Portable UNUSED parameter macro used on function signature for C and C++

Example:

int somefunction(int a, int b, int c)
{
(void)(c); // c is reserved for future usage,
//stop the compiler from issuing "unused parameter" warning
return a+b;
}

G++ Unused Label Warning for Preprocessor Macro

from what I can tell, the initialize label is passed to __TRACE__ where it's used as an argument for a printf() call.

No, it is not, actually. The x parameter of STATE() is not the same as the x parameter of __TRACE__().

In the statement STATE(initialize), the x parameter is initialize, so x: becomes simply initialize: (the label in question), but #x stringifies the input value of x as "initialize" in this case, so STATE(initialize) expands to this:

initialize: __TRACE__("enter", "initialize");

And then, in the __TRACE__ macro, the y parameter is "enter" and the x parameter is "initialize", so __TRACE__("enter", "initialize") expands to this:

dbg.printf(DebugIO::debug2,"FSM:" "enter" "(" "initialize" ")\n");

And lastly, string literals that are separated by only whitespace are merged together by the compiler, so the final code for STATE(initialize) looks like this:

initialize: dbg.printf(DebugIO::debug2,"FSM:enter(initialize)\n");;

And since there is no goto or other statement that references the initialize label, that is why you get a warning about it.

How would I correct this?

Unless there is an actual goto initialize statement in the code somewhere, I would just get rid of the label altogether:

#define STATE(x)    __TRACE__("enter", #x);


Related Topics



Leave a reply



Submit