Reputation: 1150
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
.....lots of code here not using param
}
Getting error for compilation and I don't want warning even in code.
Upvotes: 2
Views: 5008
Reputation: 2965
How about: static_cast<void>(param);
Use same method also used to discard result marked [[nodiscard]]
:
other than a cast to void, the compiler is encouraged to issue a warning.
[[nodiscard]] bool is_good_idea();
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#else
// suppress warning:
static_cast<void>(param);
#endif
// also valid:
static_cast<void>(is_good_idea());
}
Compiler won't produce any additional instructions :)
Upvotes: -1
Reputation: 595782
Prior to C++17's [[maybe_unused]]
attribute, you can use compiler-specific commands.
In MSVC, you can do this:
#pragma warning(push)
#pragma warning(disable:4101)
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
#pragma warning(pop)
In Borland/Embarcadero, you can do this:
#pragma argsused
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
In GCC, you can use one of these:
void func(__attribute__((unused)) char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
void func(__unused char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
void func([[gnu::unused]] char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
Or, you could simply reference the parameter without using any compiler-specific tricks:
void func(char * param)
{
#ifdef _XYZ_
.....somecode here using param
#else
(void)param;
#endif
}
Upvotes: 1
Reputation: 26282
Since C++17 we have the maybe_unused
attribute to suppress warnings on unused entities:
void func([[maybe_unused]] char * param)
{
#ifdef _XYZ_
.....somecode here using param
#endif
}
Upvotes: 6
Reputation: 7482
It's simple. If the _XYZ_
is NOT defined the the variable param
is not used in the function and the compiler issue a waning that can be translated to:
Translated from compilerian:
My friend you asked me for a piece of memory named
param
but you are not using it. Are you sure this is intended?
Upvotes: 2