Need your experience: is "(void)param;" to avoid unused variablewarnings well known for you?

D

Dombo

Op 02-Apr-12 13:16, Qi schreef:
Hi all,

Let me show some code first,

void someFunction(int param)
{
(void)param; // param is not used in this function. This line is
// just to silence the compiler warning about unused param.
}

Please note the "(void)param".
Is that usage common or well known for you?
Or you rarely saw it? Or even it's weird to you?

I have seen it before, but not frequently and usually wrapped in macro.
Just your experience is enough, no need to debate for it's good
or bad. :)

The reason I ask this is, in my another thread, some very veteran
C++ developers said they rarely saw that kind of usage.
That makes me a little nervous because I used it a lot in my
code to *improve* the code readability.
If it's not well known trick then it reduces code readability
and then I'm going to change my code about that.

That's not only about coding style but also about code readability.

I agree that code readability (easy to understand) is very important.
Personally I prefer to just comment out the parameter name. It is less
typing and is pretty clear even to less experienced coders. The purpose
of (void)param; will not be obvious to everyone,unless wrapped in a
macro with an obvious name.
 
S

Stuart Redmann

Hi all,

Let me show some code first,

void someFunction(int param)
{
    (void)param; // param is not used in this function. This line is
// just to silence the compiler warning about unused param.

}

Please note the "(void)param".
Is that usage common or well known for you?
Or you rarely saw it? Or even it's weird to you?

Never seen it in 15 years of C++ programming. Looks also weird to me.

Regards,
Stuart
 
Q

Qi

Thanks all for your input.

Here is my update.

I did a search, found more than 400 (void)param in about
35K lines of code.
I removed more than 200, and found it really worth my time.
Because I found even I (void) some parameters, latter
I added code to use the parameter but forgot to remove
the (void). That's terribly confusing and significantly
reduced code readability.

Now I use the commented out parameter name,
void func(int /*param*/);
Then there is no way to use an "unused" parameter.
Safe and clear.

Also I removed parameters that will never be used.

There are still more than 100 (void) in my code.
Most are in tool-generate code so I won't fix them
otherwise I need to change the tool a lot.
Others are in unit test that less harmful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top