Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Get rid of unused parameter warning?

Reply
Thread Tools

Get rid of unused parameter warning?

 
 
Rui Maciel
Guest
Posts: n/a
 
      09-25-2010
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
 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      09-25-2010
On Sep 25, 3:08*pm, Rui Maciel <rui.mac...@gmail.com> 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.
 
Reply With Quote
 
 
 
 
Alf P. Steinbach /Usenet
Guest
Posts: n/a
 
      09-25-2010
* 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>
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      09-25-2010
Öö 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
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      09-25-2010
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-25-2010
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
 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-25-2010
Öö Tiib wrote:

> On Sep 25, 3:08 pm, Rui Maciel <rui.mac...@gmail.com> 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
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      09-25-2010
On Sep 25, 4:28*pm, Rui Maciel <rui.mac...@gmail.com> 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.
 
Reply With Quote
 
Rui Maciel
Guest
Posts: n/a
 
      09-25-2010
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
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      09-26-2010
Öö 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."
 
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
Parameter List / Parameter Block / Anything patterns... mast2as@yahoo.com C++ 4 03-29-2007 09:37 PM
Clean up "unused parameter" compiler warnings? Charles Sullivan C Programming 11 09-11-2006 09:34 AM
how do I get rid of beeping sound every time I get a new email? joe54345@gmail.com Computer Support 13 12-06-2005 10:14 PM
is it possible to get hidden parameter values using window.opener.document.form.parameter.value. Abdul Mohsin Javascript 1 09-06-2005 03:38 PM
Supressing Unused Parameter Warnings Michael B Allen C Programming 11 05-08-2004 08:21 AM



Advertisments