Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #15137

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

From Victor Bazarov <v.bazarov@comcast.invalid>
Newsgroups comp.lang.c++
Subject Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you?
Date 2012-04-02 09:49 -0400
Organization A noiseless patient Spider
Message-ID <jlcaok$vsl$1@dont-email.me> (permalink)
References <jlc1qv$kjj$2@speranza.aioe.org> <jlc366$l8s$1@news.albasani.net>

Show all headers | View raw


On 4/2/2012 7:39 AM, Fred Zwarts (KVI) wrote:
> "Qi" wrote in message news:jlc1qv$kjj$2@speranza.aioe.org...
>>
>> 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

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Qi <no@no.com> - 2012-04-02 19:16 +0800
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? "Fred Zwarts \(KVI\)" <F.Zwarts@KVI.nl> - 2012-04-02 13:39 +0200
    Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-04-02 09:49 -0400
      Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Qi <no@no.com> - 2012-04-02 22:01 +0800
        Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Victor Bazarov <v.bazarov@comcast.invalid> - 2012-04-02 11:50 -0400
        Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Paul N <gw7rib@aol.com> - 2012-04-02 12:54 -0700
      Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Juha Nieminen <nospam@thanks.invalid> - 2012-04-03 05:27 +0000
        Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-04-03 08:02 +0000
          Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Juha Nieminen <nospam@thanks.invalid> - 2012-04-03 14:19 +0000
    Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Mike McCarty <mcmccarty@gmail.com> - 2012-04-02 06:43 -0700
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? GM <GM@no.spam.it> - 2012-04-02 14:41 +0200
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? bartek szurgot <basz@no.spam> - 2012-04-02 16:55 +0200
    Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Juha Nieminen <nospam@thanks.invalid> - 2012-04-03 05:28 +0000
      Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Qi <no@no.com> - 2012-04-03 13:54 +0800
        Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Juha Nieminen <nospam@thanks.invalid> - 2012-04-03 14:09 +0000
          Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Robert Wessel <robertwessel2@yahoo.com> - 2012-04-03 11:21 -0500
            Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? bartek szurgot <basz@no.spam> - 2012-04-04 17:39 +0200
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Jorgen Grahn <grahn+nntp@snipabacken.se> - 2012-04-02 15:13 +0000
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? "MikeWhy" <boat042-nospam@yahoo.com> - 2012-04-02 11:37 -0500
    Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Qi <no@no.com> - 2012-04-03 10:29 +0800
      Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? "Alf P. Steinbach" <alf.p.steinbach+usenet@gmail.com> - 2012-04-03 06:00 +0200
        Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Qi <no@no.com> - 2012-04-03 13:56 +0800
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? goran.pusic@gmail.com - 2012-04-02 04:25 -0700
    Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? scott@slp53.sl.home (Scott Lurndal) - 2012-04-02 22:01 +0000
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Öö Tiib <ootiib@hot.ee> - 2012-04-02 07:37 -0700
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Dombo <dombo@disposable.invalid> - 2012-04-03 19:35 +0200
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Stuart Redmann <DerTopper@web.de> - 2012-04-04 11:56 -0700
  Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you? Qi <no@no.no> - 2012-04-05 14:17 +0800

csiph-web