Get rid of unused parameter warning?

Discussion in 'C++' started by Rui Maciel, Sep 25, 2010.

  1. Rui Maciel

    Rui Maciel Guest

    I have an abstract base class which defines an interface to a function which takes 3 arguments.
    From that ABC I define a set of derived classes which implements the base class' interface. The
    thing is, some derived classes don't use some of the parameters, which causes the compiler to throw
    warnings. As I'm not using those parameters intentionally, those warnings tend to be a bit
    annoying.

    So, does anyone happen to know a good standard way to get the compiler to stop warning about those
    specific instances where a parameter isn't used?


    Thanks in advance,
    Rui Maciel
    Rui Maciel, Sep 25, 2010
    #1
    1. Advertising

  2. Rui Maciel

    Öö Tiib Guest

    On Sep 25, 3:08 pm, Rui Maciel <> wrote:
    > I have an abstract base class which defines an interface to a function which takes 3 arguments.  
    > From that ABC I define a set of derived classes which implements the base class' interface.  The
    > thing is, some derived classes don't use some of the parameters, which causes the compiler to throw
    > warnings.  As I'm not using those parameters intentionally, those warnings tend to be a bit
    > annoying.
    >
    > So, does anyone happen to know a good standard way to get the compiler to stop warning about those
    > specific instances where a parameter isn't used?


    One way is not to name the parameters, give only type:

    int foo( int )
    {
    return 0;
    }

    For me it is fine. The downside that some claim it not documenting the
    intention well enough. They may use some macro or template that does
    not generate instructions:

    template<typename T>
    inline void UnusedParameter( T const& )
    {
    }

    #define UNUSED_PARAMETER(X) sizeof(X)

    int foo( int a, int b )
    {
    UNUSED_PARAMETER(a);
    UnusedParameter(b);
    return 0;
    }

    That perhaps documents everything well enough.
    Öö Tiib, Sep 25, 2010
    #2
    1. Advertising

  3. * Rui Maciel, on 25.09.2010 14:08:
    > I have an abstract base class which defines an interface to a function
    > which takes 3 arguments. From that ABC I define a set of derived classes
    > which implements the base class' interface. The thing is, some derived
    > classes don't use some of the parameters, which causes the compiler to
    > throw warnings. As I'm not using those parameters intentionally, those
    > warnings tend to be a bit annoying.
    >
    > So, does anyone happen to know a good standard way to get the compiler to
    > stop warning about those specific instances where a parameter isn't used?


    In addition to Öö's answer else-thread you might consider this an opportunity
    for redesign. For example, perhaps a parameter that is unused in one
    implementation but used in another, is really a piece of knowledge that should
    reside with the object that hosts the function implementation that uses the
    parameter? Or perhaps there should be two different functions, not just one?

    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 25, 2010
    #3
  4. Rui Maciel

    Rui Maciel Guest

    Öö Tiib wrote:

    > One way is not to name the parameters, give only type:
    >
    > int foo( int )
    > {
    > return 0;
    > }
    >
    > For me it is fine.

    <snip/>

    Thanks for the help, Öö. That does the trick quite nicely, at least on g++. Is this trick
    guaranteed to work on all platforms or is it one of those things specific to GCC?


    Once again thanks for the help. Kudos!
    Rui Maciel
    Rui Maciel, Sep 25, 2010
    #4
  5. Rui Maciel

    Rui Maciel Guest

    Alf P. Steinbach /Usenet wrote:

    > In addition to Öö's answer else-thread you might consider this an
    > opportunity for redesign. For example, perhaps a parameter that is unused
    > in one implementation but used in another, is really a piece of knowledge
    > that should reside with the object that hosts the function implementation
    > that uses the parameter? Or perhaps there should be two different
    > functions, not just one?


    I see what you mean and you do have a point. Nonetheless, in this case I believe that this design
    works well. The abstract base class is used to specify, among other things, the interfaces to a
    specific family of interpolation functions in 3D space and their partial derivatives. The
    interpolation functions are defined in the subsequent derived classes. As some functions happen to
    be linear polynomials, that means that deriving them results in eliminating a parameter from the
    expression, which causes the compiler to warn about unused parameters.


    Rui Maciel
    Rui Maciel, Sep 25, 2010
    #5
  6. Rui Maciel

    Kai-Uwe Bux Guest

    Rui Maciel wrote:

    > Öö Tiib wrote:
    >
    >> One way is not to name the parameters, give only type:
    >>
    >> int foo( int )
    >> {
    >> return 0;
    >> }
    >>
    >> For me it is fine.

    > <snip/>
    >
    > Thanks for the help, Öö. That does the trick quite nicely, at least on
    > g++. Is this trick guaranteed to work on all platforms or is it one of
    > those things specific to GCC?


    As with all warnings, they are compiler specific. There is nothing in the
    standard that requires the warnings in the first place, and there is also no
    language that prevents a compiler from issuing gratuitous warnings of any
    kind.

    That said, what would be a good wording for this warning in case of an
    unnamed parameter? Something like:

    unnamed parameter not used (not that you
    could use it, but I just felt like telling you)


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 25, 2010
    #6
  7. Rui Maciel

    Kai-Uwe Bux Guest

    Öö Tiib wrote:

    > On Sep 25, 3:08 pm, Rui Maciel <> wrote:
    >> I have an abstract base class which defines an interface to a function
    >> which takes 3 arguments. From that ABC I define a set of derived classes
    >> which implements the base class' interface. The thing is, some derived
    >> classes don't use some of the parameters, which causes the compiler to
    >> throw warnings. As I'm not using those parameters intentionally, those
    >> warnings tend to be a bit annoying.
    >>
    >> So, does anyone happen to know a good standard way to get the compiler to
    >> stop warning about those specific instances where a parameter isn't used?

    >
    > One way is not to name the parameters, give only type:
    >
    > int foo( int )
    > {
    > return 0;
    > }
    >
    > For me it is fine.


    For me too.

    > The downside that some claim it not documenting the
    > intention well enough. They may use some macro or template that does
    > not generate instructions:
    >
    > template<typename T>
    > inline void UnusedParameter( T const& )
    > {
    > }
    >
    > #define UNUSED_PARAMETER(X) sizeof(X)
    >
    > int foo( int a, int b )
    > {
    > UNUSED_PARAMETER(a);
    > UnusedParameter(b);
    > return 0;
    > }
    >
    > That perhaps documents everything well enough.


    I think, this is worse. By giving names to the parameters, you actually make
    it possible in the first place to use them. Those macros amount to nothing
    more than comments and, given enough time, they _will_ turn false. The
    compiler has no change anymore of catching things like:

    int foo ( int a, int b )
    {
    UNUSED_PARAMETER(a);
    return ( a*b );
    }


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 25, 2010
    #7
  8. Rui Maciel

    Öö Tiib Guest

    On Sep 25, 4:28 pm, Rui Maciel <> wrote:
    > Öö Tiib wrote:
    > > One way is not to name the parameters, give only type:

    >
    > > int foo( int )
    > > {
    > > return 0;
    > > }

    >
    > > For me it is fine.

    >
    > <snip/>
    >
    > Thanks for the help, Öö. That does the trick quite nicely, at least on g++.  Is this trick
    > guaranteed to work on all platforms or is it one of those things specific to GCC?


    I don't know a C++ compiler that does not compile it.
    Öö Tiib, Sep 25, 2010
    #8
  9. Rui Maciel

    Rui Maciel Guest

    Christian Hackl wrote:

    > What's wrong with disabling the warning using appropriate compiler flags
    > for those compilation units in which the warning annoys you?


    I only want to eliminate that particular type of warning when it refers to the set of functions
    which I intentionally wrote so that they don't use a given parameter.


    Rui Maciel
    Rui Maciel, Sep 25, 2010
    #9
  10. Öö Tiib <> wrote:
    >> Thanks for the help, Öö. That does the trick quite nicely, at least on g++.  Is this trick
    >> guaranteed to work on all platforms or is it one of those things specific to GCC?

    >
    > I don't know a C++ compiler that does not compile it.


    I think that's the wrong answer to the question. The correct answer is:
    "Well, the C++ standard defines that syntax. If a compiler wouldn't support
    it, throw it away because it's broken."
    Juha Nieminen, Sep 26, 2010
    #10
  11. Rui Maciel

    Öö Tiib Guest

    On 26 sept, 10:11, Juha Nieminen <> wrote:
    > Öö Tiib <> wrote:
    > >> Thanks for the help, Öö. That does the trick quite nicely, at least on g++.  Is this trick
    > >> guaranteed to work on all platforms or is it one of those things specific to GCC?

    >
    > > I don't know a C++ compiler that does not compile it.

    >
    >   I think that's the wrong answer to the question. The correct answer is:
    > "Well, the C++ standard defines that syntax. If a compiler wouldn't support
    > it, throw it away because it's broken."


    Maybe your answer is better, but it does not say what i wanted to
    tell. I wanted only to say that

    int foo( int )
    {
    return 0;
    }

    compiles well and without warnings on all C++ compilers that i know of
    and so that is not g++ specific. Not everything may be anonymous by
    standard. Code like:

    typedef struct {int x; int y} Point;

    It is ill-formed (but does still compile on several compilers).
    Öö Tiib, Sep 26, 2010
    #11
  12. Öö Tiib <> wrote:
    > typedef struct {int x; int y} Point;
    >
    > It is ill-formed (but does still compile on several compilers).


    Hmm, why is it ill-formed? (Or are you talking about the missing
    semi-colon?)
    Juha Nieminen, Sep 26, 2010
    #12
  13. On 9/25/2010 1:12 PM, Rui Maciel wrote:
    > Christian Hackl wrote:
    >
    >> What's wrong with disabling the warning using appropriate compiler flags
    >> for those compilation units in which the warning annoys you?

    >
    > I only want to eliminate that particular type of warning when it refers to the set of functions
    > which I intentionally wrote so that they don't use a given parameter.


    Why do you give the function a parameter when you know that you're not
    going to use it? Just curious.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 27, 2010
    #13
  14. Rui Maciel

    Rui Maciel Guest

    Victor Bazarov wrote:

    > Why do you give the function a parameter when you know that you're not
    > going to use it? Just curious.


    In this case we are dealing with an abstract base class which defines a specific interface. Then,
    when implementing this interface, some implementations may not use every parameter which was defined
    in the interface. In my case, I was dealing with an interface to a set of interpolation functions,
    along with a set of partial derivatives. As some interpolation functions happen to be linear
    polynomials, their derived functions will not contain some parameters, hence the need to get rid of
    those warnings.


    Rui Maciel
    Rui Maciel, Sep 27, 2010
    #14
  15. Rui Maciel

    debra h Guest

    On Sep 25, 5:08 pm, Rui Maciel <> wrote:
    > I have an abstract base class which defines an interface to a function which takes 3 arguments.  
    > From that ABC I define a set of derived classes which implements the base class' interface.  The
    > thing is, some derived classes don't use some of the parameters, which causes the compiler to throw
    > warnings.  As I'm not using those parameters intentionally, those warnings tend to be a bit
    > annoying.
    >
    > So, does anyone happen to know a good standard way to get the compiler to stop warning about those
    > specific instances where a parameter isn't used?
    >
    > Thanks in advance,
    > Rui Maciel


    Thank you so much for sharing information.
    debra h, Oct 1, 2010
    #15
    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. Ulrich Eckhardt
    Replies:
    3
    Views:
    333
  2. Grant Edwards
    Replies:
    3
    Views:
    576
    Grant Edwards
    Aug 11, 2004
  3. Vittorix
    Replies:
    5
    Views:
    334
    Vittorix
    Nov 17, 2006
  4. pkirk25
    Replies:
    5
    Views:
    432
    Chris Dollin
    Sep 27, 2006
  5. Replies:
    2
    Views:
    313
    Fred Kleinschmidt
    Dec 18, 2006
Loading...

Share This Page