Jump to content
  • Advertisement
Sign in to follow this  
Alundra

Unused param

This topic is 1350 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

I always used this macro to remove warning when needed :

#define UNUSED(x) static_cast<void>(x)

But I recently see, a template version could be used like that :

template<class T> INLINE void UNUSED(const T&){}

What is the best to use and why ? I read Intel compiler didn't like static_cast to remove warning, but apparently now it does from other post.

Thanks

Edited by Alundra

Share this post


Link to post
Share on other sites
Advertisement

Unless I'm in the situation where the parameter is used in only debug or only release build, I tend to just comment out the parameter name.

 

I've also seen people just mention the variable name in the function body on a line by itself, without any cast.

Share this post


Link to post
Share on other sites
The template you posted would itself cause a slightly different warning on one compiler I use, as it is itself a function with an unused argument.

Share this post


Link to post
Share on other sites
void FunkySmell( int _iUnused ) {
}
// Warning, unused parameter.

void FunkySmell( int _iUnused ) {
    static_cast<void>(_iUnused);
}
// Warning, useless statement (no side-effect).

void FunkySmell( int /*_iUnused*/ ) {
}
// Correct.

L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

I see.
But I am fairly sure (entirely open to correction) that your cast produces the same result as my static cast, which generates a warning in Xcode.
No matter what, Coverity is going to flag these things.


I’m not sure of any way to avoid these kinds of warnings entirely (and we are not having a discussion on why we are putting “useless” parameters into functions because there are so many legitimate reasons why it happens), so your best bet is just to figure out what reduces the number of warnings you yourself get while compiling.

Hodgman and I have our favorite ways. They don’t always eliminate the problems, especially when you are supporting lots of platforms or even just overloading operator new.  Right now your best course of action is simply to do what it takes to remove the warnings that you are encountering.  I stand by my proposal, but I literally don’t think any course of action will pacify every compiler out there.  For now it makes more sense to just do what pacifies most of them, and if you encounter some remaining issues then ask again, specifying which compiler you are using and what warning you aim to avoid.

 

 

L. Spiro

Share this post


Link to post
Share on other sites

If no real solution, an if-else checking the compilator can always gives the trick, actually static_cast remove all of these warnings on msvc,gcc and clang.

I didn't test myself intel compilator but apparently now the template version is not needed, the static_cast seems to remove the warning too.

Edited by Alundra

Share this post


Link to post
Share on other sites

With VC++ I use the macro UNREFERENCED_PARAMETER(x).

 

I've just had a quick peek and it's defined as the following.

#define UNREFERENCED_PARAMETER(P)          (P)

Edited by collie

Share this post


Link to post
Share on other sites


and we are not having a discussion on why we are putting “useless” parameters into functions because there are so many legitimate reasons why it happens

I'm not sure I buy the 'legitimate' part of that. As I see it, there are usually two cases where we end up with unused parameters:

  • When the implementation has changed such that the public API no longer accurately reflects the provided functionality.
  • When you abuse polymorphism such that a child must override a parent method to do something unrelated.

Neither of those are particularly legitimate - the former can be addressed via deprecation, and the latter by refactoring.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!