Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Need your experience: is "(void)param;" to avoid unused variablewarnings well known for you?

Reply
Thread Tools

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

 
 
Qi
Guest
Posts: n/a
 
      04-02-2012
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?

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.

Looking forward to hearing your experience.

Thanks


--
WQ
 
Reply With Quote
 
 
 
 
goran.pusic@gmail.com
Guest
Posts: n/a
 
      04-02-2012
On Monday, April 2, 2012 1:16:30 PM UTC+2, Qi wrote:
> 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?


Seen it. Also seen macros, coming from Microsoft, that use this trick and distinguish between NDEBUG and _DEBUG builds.
 
Reply With Quote
 
 
 
 
Fred Zwarts \(KVI\)
Guest
Posts: n/a
 
      04-02-2012
"Qi" wrote in message news:jlc1qv$kjj$(E-Mail Removed)...
>
>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?
>
>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.
>
>Looking forward to hearing your experience.
>
>Thanks


I have never seen this trick. Why don't you use the standard C++ way? Remove
the name of the parameter and the compiler will stop complaining. Less code,
better readability. The "(void)param" makes me wonder what the purpose is.
You will need comments to explain why it is used. If the parameter name is
omitted, it is immediately clear that it is not used in the function. No
comments needed. For the reason why it is not used in the function, comments
may be needed in both cases.

void someFunction(int)
{

//
}

 
Reply With Quote
 
Mike McCarty
Guest
Posts: n/a
 
      04-02-2012
On Monday, April 2, 2012 6:39:50 AM UTC-5, F.Zwarts wrote:
> I have never seen this trick. Why don't you use the standard C++ way? Remove
> the name of the parameter and the compiler will stop complaining. Less code,
> better readability. The "(void)param" makes me wonder what the purpose is.
> You will need comments to explain why it is used. If the parameter name is
> omitted, it is immediately clear that it is not used in the function. No
> comments needed. For the reason why it is not used in the function, comments
> may be needed in both cases.
>
> void someFunction(int)
> {
>
> //
> }


Usually this only a problem in #ifdef'ed code. E.g.

void someFunction(int wparam)
{
#ifdef _WIN32
...
#else
// Only need this parameter on Windows.
(void)wparam;
#endif
}

This works on most compilers but the only technique that I know of that works on all compilers is this:

namespace my {
template <typename T> inline void unused(const T&) {}
}

....

my::unused(wparam);


Mike
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-02-2012
On 4/2/2012 7:39 AM, Fred Zwarts (KVI) wrote:
> "Qi" wrote in message news:jlc1qv$kjj$(E-Mail Removed)...
>>
>> 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?
>>
>> 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.
>>
>> Looking forward to hearing your experience.
>>
>> Thanks

>
> I have never seen this trick. Why don't you use the standard C++ way?
> Remove the name of the parameter and the compiler will stop complaining.
> Less code, better readability. The "(void)param" makes me wonder what
> the purpose is. You will need comments to explain why it is used. If the
> parameter name is omitted, it is immediately clear that it is not used
> in the function. No comments needed. For the reason why it is not used
> in the function, comments may be needed in both cases.
>
> void someFunction(int)
> {
>
> //
> }


I've seen that trick usually used in (a) a larger function than just one
empty line and one line of comment, and (b) in presence of some reason
to keep the argument name, like alternative configurations
(Debug/Release for instance):

void someFunction(int argumentUsedOnlyInDebug)
{
... // some code
#ifdef NDEBUG
(void)argumentUsedOnlyInDebug;
#else
... // some code that uses argumentUsedOnlyInDebug
#endif
... // more code
}

Any suggestions for a better alternative?

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Qi
Guest
Posts: n/a
 
      04-02-2012
On 2012-4-2 21:49, Victor Bazarov wrote:
>
> I've seen that trick usually used in (a) a larger function than just one
> empty line and one line of comment, and (b) in presence of some reason
> to keep the argument name, like alternative configurations
> (Debug/Release for instance):


I don't understand (a).
Can you give some example code?

> void someFunction(int argumentUsedOnlyInDebug)
> {
> ... // some code
> #ifdef NDEBUG
> (void)argumentUsedOnlyInDebug;
> #else
> ... // some code that uses argumentUsedOnlyInDebug
> #endif
> ... // more code
> }
>
> Any suggestions for a better alternative?


I think one more readable way is using some marco?

#define UNUSED_ARG(arg) (void)arg

UNUSED_ARG(argumentUsedOnlyInDebug);

It's less confusing than
(void)argumentUsedOnlyInDebug;


--
WQ
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      04-02-2012
On Apr 2, 2:16*pm, Qi <(E-Mail Removed)> wrote:
> 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. I understand what it does. Most often such functions
are still the stubs, "//TODO: implement it" and in such stage i don't
really care if and how the author silences the warnings.

A ready made function has both declaration (in header file) and
definition (in implementation file). I have seen that the declaration
contains the names even for unused parameters and definition does not
contain them. This is usually good enough in circumstances where the
reasons why the parameters are unused are obvious. For example a dummy
of unit tests:

All parameters unnamed and functions body ... something like ... throw
std::logic_error("*function_name_here* should not be called during
these tests.");. Everything is clear what and why.

> 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.


May be you should discuss the cases when and why you need these unused
parameters instead?

I have feeling that something is wrong when the unused function
parameters are so frequent in a real production code of C++ project
that "a lot" can be said and special tricks are needed. The parameters
are passed with a single purpose to waste cycles so the warning feels
to be in place there. You should consider ways how to have minimum
amount of such useless parameters. That improves both readability of
your code and performance of your product.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-02-2012
On Mon, 2012-04-02, Qi wrote:
> 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 see it a lot in C code (sometimes hidden inside a UNUSED(param)
macro). I don't think I see it in C++ code, but I don't read much of
other people's C++ code.

> 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.


I don't understand your reasoning. Why would the above be more
readable than

void someFunction(int) // parameter obviously unused
{}

or

void someFunction() // parameter so unused it doesn't even exist
{}

?

> That's not only about coding style but also about code readability.
>
> Looking forward to hearing your experience.


/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-02-2012
On 4/2/2012 10:01 AM, Qi wrote:
> On 2012-4-2 21:49, Victor Bazarov wrote:
>>
>> I've seen that trick usually used in (a) a larger function than just one
>> empty line and one line of comment, and (b) in presence of some reason
>> to keep the argument name, like alternative configurations
>> (Debug/Release for instance):

>
> I don't understand (a).
> Can you give some example code?


*You* gave the example code. In your code the function had effectively
an empty body. In that case there is no need to have "ignore"
templates, "UNUSED_PARAMETER" macros, or any other BS. Just drop the
argument altogether.

I am talking any reasonable function that actually is supposed to do
something. The "code example" is below.

>
>> void someFunction(int argumentUsedOnlyInDebug)
>> {
>> ... // some code
>> #ifdef NDEBUG
>> (void)argumentUsedOnlyInDebug;
>> #else
>> ... // some code that uses argumentUsedOnlyInDebug
>> #endif
>> ... // more code
>> }
>>
>> Any suggestions for a better alternative?

>
> I think one more readable way is using some marco?
>
> #define UNUSED_ARG(arg) (void)arg
>
> UNUSED_ARG(argumentUsedOnlyInDebug);
>
> It's less confusing than
> (void)argumentUsedOnlyInDebug;


<shrug> Same difference.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
MikeWhy
Guest
Posts: n/a
 
      04-02-2012

"Qi" <(E-Mail Removed)> wrote in message news:jlc1qv$kjj$(E-Mail Removed)...
> 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?
>
> Just your experience is enough, no need to debate for it's good
> or bad.


I say "NOT GOOD". It does indeed silence the compiler warning, but does not
prevent subsequent use of that param, contrary to the expectations set by
silencing the warning.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
survey: cool but not so well-known python apps Alia Khouri Python 0 04-26-2009 12:42 PM
any way to avoid warnings about unused outputs in XST? Ken Cecka VHDL 10 03-02-2009 10:51 PM
Re: Is this phase-accumulator trick well-known??? Mike Treseler VHDL 0 02-08-2009 07:17 PM
List mutation method gotcha - How well known? Hendrik van Rooyen Python 20 03-16-2008 09:55 AM
well-known Internet sites which use Java based web servers Mladen Adamovic Java 3 10-24-2005 09:48 PM



Advertisments