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 asassert
.((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)
andsizeof
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 aprintf()
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
Best Introduction to C++ Template Metaprogramming
Why am I Getting String Does Not Name a Type Error
Declare Variables at Top of Function or in Separate Scopes
How Would You Implement a Basic Event-Loop
Why Is F(I = -1, I = -1) Undefined Behavior
When to Make a Type Non-Movable in C++11
Thread Safe Lazy Construction of a Singleton in C++
Is There a Name for This Tuple-Creation Idiom
Why Is Cuda Pinned Memory So Fast
Clean C++ Granular Friend Equivalent? (Answer: Attorney-Client Idiom)
App Does Not Run with VS 2008 Sp1 Dlls, Previous Version Works with Rtm Versions
Is There Any Standard Way of Embedding Resources into Linux Executable Image
Is "Inline" Implicit in C++ Member Functions Defined in Class Definition
How to Use Identical Names for Fields and Constructor Parameters
C++ Function Callbacks: Cannot Convert from a Member Function to a Function Signature