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

Discussion in 'C++' started by Qi, Apr 2, 2012.

  1. Qi

    Qi Guest

    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
     
    Qi, Apr 2, 2012
    #1
    1. Advertising

  2. Qi

    Guest

    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.
     
    , Apr 2, 2012
    #2
    1. Advertising

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

    "Qi" wrote in message news:jlc1qv$kjj$...
    >
    >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)
    {

    //
    }
     
    Fred Zwarts \(KVI\), Apr 2, 2012
    #3
  4. Qi

    Mike McCarty Guest

    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
     
    Mike McCarty, Apr 2, 2012
    #4
  5. On 4/2/2012 7:39 AM, Fred Zwarts (KVI) wrote:
    > "Qi" wrote in message news:jlc1qv$kjj$...
    >>
    >> 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
     
    Victor Bazarov, Apr 2, 2012
    #5
  6. Qi

    Qi Guest

    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
     
    Qi, Apr 2, 2012
    #6
  7. Qi

    Öö Tiib Guest

    On Apr 2, 2:16 pm, 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 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.
     
    Öö Tiib, Apr 2, 2012
    #7
  8. Qi

    Jorgen Grahn Guest

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

    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 .
     
    Jorgen Grahn, Apr 2, 2012
    #8
  9. 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
     
    Victor Bazarov, Apr 2, 2012
    #9
  10. Qi

    MikeWhy Guest

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

    "Qi" <> wrote in message news:jlc1qv$kjj$...
    > 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.
     
    MikeWhy, Apr 2, 2012
    #10
  11. Qi

    Paul N Guest

    On Apr 2, 3:01 pm, 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?
    >
    > > 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;


    I'd be wary of code that said UNUSED_ARG(arg); and then went ahead to
    use arg. Perhaps POSSIBLY_UNUSED_ARG(arg); would be better in this
    situation?
     
    Paul N, Apr 2, 2012
    #11
  12. Qi

    Qi Guest

    On 2012-4-3 0:37, MikeWhy wrote:
    >
    > 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.


    I did something like that.
    I (void)param first, then later I added some code to use param.
    Really not good and confusing...


    --
    WQ
     
    Qi, Apr 3, 2012
    #12
  13. On 03.04.2012 04:29, Qi wrote:
    > On 2012-4-3 0:37, MikeWhy wrote:
    >>
    >> 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.

    >
    > I did something like that.
    > I (void)param first, then later I added some code to use param.
    > Really not good and confusing...


    Some days ago I posted the following to the thread "Any way to defer
    template function overloading?":


    <code>
    #if !defined( CPP_DECLARE_UNUSED )
    # define CPP_DECLARE_UNUSED( argName ) \
    (void)(argName, void(), 0); struct argName; (void)0
    #endif
    // SO user sehe: "(void)0" for semicolon
    // SO user R. Martinho Fernandes, pointed out operator void() problem
    // SO user Johannes Schaub, solution to operator,() problem
    </code>


    It ensures that a param designated as unused, is not used.

    It seems to me that C++ questions are sort of like memes: suddenly the
    same question pops up all over the place, and then it dies out, sort of.


    Cheers & hth.,

    - Alf
     
    Alf P. Steinbach, Apr 3, 2012
    #13
  14. Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you?

    Victor Bazarov <> wrote:
    > 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?


    Since the above situation is extremely rare, you could use the trick
    in that specific situation (which in practice means almost never) and
    add a clarifying comment to it.

    Btw, do compilers by convention not issue a warning about a no-op line
    like that?
     
    Juha Nieminen, Apr 3, 2012
    #14
  15. Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you?

    bartek szurgot <> wrote:
    > there is just one issue with this. when you use some variable only in
    > debug, but not in release, in the second case you still get warning. for
    > example:
    >
    > void doSth(const char *name)
    > {
    > assert(name!=NULL);
    > // no other usages of 'name'
    > }


    A compiler should be smart enough to not give a warning in the above
    case even if NDEBUG is defined (because, after all, the variable *is*
    used: As a parameter to a macro.)
     
    Juha Nieminen, Apr 3, 2012
    #15
  16. Qi

    Qi Guest

    On 2012-4-3 13:28, Juha Nieminen wrote:
    >>
    >> void doSth(const char *name)
    >> {
    >> assert(name!=NULL);
    >> // no other usages of 'name'
    >> }

    >
    > A compiler should be smart enough to not give a warning in the above
    > case even if NDEBUG is defined (because, after all, the variable *is*
    > used: As a parameter to a macro.)


    When compiler sees the code, that line has gone if NDEBUG is defined
    because it's removed by preprocessor if assert is empty when NDEBUG
    is defined.
    So there is still warning, no?


    --
    WQ
     
    Qi, Apr 3, 2012
    #16
  17. Qi

    Qi Guest

    On 2012-4-3 12:00, Alf P. Steinbach wrote:
    >
    > It seems to me that C++ questions are sort of like memes: suddenly the
    > same question pops up all over the place, and then it dies out, sort of.


    Not really. My other thread is about template function overloading.
    This thread is the only one I started about unused parameter.


    --
    WQ
     
    Qi, Apr 3, 2012
    #17
  18. Qi

    Jorgen Grahn Guest

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

    On Tue, 2012-04-03, Juha Nieminen wrote:
    > Victor Bazarov <> wrote:
    >> 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?

    >
    > Since the above situation is extremely rare, you could use the trick
    > in that specific situation (which in practice means almost never) and
    > add a clarifying comment to it.


    Unfortunately, such code is not rare everywhere. Bad high-level design
    choices ("feature A and B have similarities; let's call them FOO and
    implement them with the same code. Only a few ifdefs needed ...") plus
    a ban on compiler warnings leads to such things.

    > Btw, do compilers by convention not issue a warning about a no-op line
    > like that?


    Yes, that's my impression. I wouldn't be surprised if it was lint that
    introduced the convention in the 1980s or earlier.

    /Jorgen

    --
    // Jorgen Grahn <grahn@ Oo o. . .
    \X/ snipabacken.se> O o .
     
    Jorgen Grahn, Apr 3, 2012
    #18
  19. Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you?

    Qi <> wrote:
    >> A compiler should be smart enough to not give a warning in the above
    >> case even if NDEBUG is defined (because, after all, the variable *is*
    >> used: As a parameter to a macro.)

    >
    > When compiler sees the code, that line has gone if NDEBUG is defined
    > because it's removed by preprocessor if assert is empty when NDEBUG
    > is defined.
    > So there is still warning, no?


    In the era where the C preprocessor, the compiler and the linker were
    all completely separate programs that might have been the case (because
    the compiler, which is the one issuing warnings, does not see the original
    source code, only the code generated by the C preprocessor).

    However, in modern compilers there really is no need to separate the
    C preprocessor from the compiler so at least in principle the compiler
    could see that "hey, this variable is given as parameter to this macro"
    and thus elide the warning.

    OTOH, I don't know if any modern compiler is that smart... I'm just
    talking hypothetically here.
     
    Juha Nieminen, Apr 3, 2012
    #19
  20. Re: Need your experience: is "(void)param;" to avoid unused variable warnings well known for you?

    Jorgen Grahn <> wrote:
    > Unfortunately, such code is not rare everywhere. Bad high-level design
    > choices ("feature A and B have similarities; let's call them FOO and
    > implement them with the same code. Only a few ifdefs needed ...") plus
    > a ban on compiler warnings leads to such things.


    Reminds me of an open source project (which shall remain unnamed)
    where the developers had the breaindead principle that, 1) all warnings
    are errors (by using the gcc flag for that), and 2) they used every single
    warning flag that they could find on gcc's info page. Naturally they
    considered these settings mandatory for official builds.

    This meant, for example, that if gcc did not inline a function that
    was marked as 'inline' you would get an error, even though that situation
    is completely inconsequential to the validity of the program. (The warning
    in question is purely informative, and not turned on with -Wall, for
    obvious reasons.)

    It also meant that the program might compile with one version of gcc but
    not with another, if they use different inlining strategies. Way to go.
     
    Juha Nieminen, Apr 3, 2012
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mladen Adamovic
    Replies:
    3
    Views:
    11,032
  2. Hendrik van Rooyen

    List mutation method gotcha - How well known?

    Hendrik van Rooyen, Mar 13, 2008, in forum: Python
    Replies:
    20
    Views:
    642
  3. Mike Treseler
    Replies:
    0
    Views:
    685
    Mike Treseler
    Feb 8, 2009
  4. Alia Khouri
    Replies:
    0
    Views:
    311
    Alia Khouri
    Apr 26, 2009
  5. Dejas
    Replies:
    5
    Views:
    233
    Bart Van der Donck
    Oct 14, 2007
Loading...

Share This Page