Why not skip templates altogether and go with the ellipses.
inline void unused (...) { /* do nothing */ }
Why not skip templates altogether and go with the ellipses.
inline void unused (...) { /* do nothing */ }
Why are you passing !readWriteActivated
instead of readWriteActivated
if the value of the expression doesn't matter?
A common (and much simpler) way to achieve this is to just cast the result to void
.
(void) x;
where x is some otherwise unreferenced value.
If you want to suppress the unused variable warning, why do you call it as
unused(!readWriteActivated); ? Why can't you just call it as
unused(readWriteActivated);
and make the code as
template<typename T>
void UnUsed(const T& )
{
}
For more references see the blog post from Herb Sutter Here
EDIT: Removed the parameter name in the function. This works for unused(!readWriteActivated); as well.
The best solution that I've seen is like this:
#define UNUSED(x) ((void)x)
It is portable, and suppresses the warning successful.
EDIT:
since you've stated that this is more like an assertion, then you should probably do something like this:
#if defined ASSERT_ENABLED
#define TEST(test) (!(test)) ? assert_failed(# test, __FILE__, __LINE__) : (void)0
#else
#define TEST(ignore) ((void)0)
#endif
This will produce no code unless ASSERT_ENABLED
is defined and won't produce a warning about unused variables. This is pretty much how the assert
macro in libc works.
I suppose the issue is that the variables are only used in the assertion, which is a poor way to do what you want. Why not mark it as unused and use the assertion macro separately that way it is clear that the variable isn't really used for anything, but you still get your debug build assertion. Just tackle the problems individually.
Change your definition of unused:
inline void unused(bool) {}
Since you already want an expression for which conversion to bool is required, this does that conversion and nothing else. Inline allows the compiler to optimize, including in situations where the expression doesn't have side-effects (but you'll have to test to know exactly what happens in complex situations).
Additionally, this fixes a common problem with most assert macros: if the expression does have side-effects, those will always be evaluated. (Depending on use, that can be very good or very bad.)
Charles Nicholson suggests doing something like this to mark unused variables for reasons explained in this article:
#define UNSUSED(a) ((void)sizeof(a))
The short version is... sizeof does not evaluate the expression, but compilers still count it as "used" when it's seen in this context.
I believe this satisfies all 4 of your criteria, specifically because sizeof() can take any valid expression, and because the expression will not be evaluated (and thus will not generate any code).
As Johannes said in the comments, you hit a compiler bug. You can work around it by explicitly converting to bool
:
unused( bool( !readWriteActivated) ); // add bool() to any (!volatile_bool_var)
If I recall the const-volatile qualification rules, all you need is to qualify the dummy variable more. Essentially, you just want to parrot the error message back in the declared type :vP .
template<typename T>
void
unused(T const volatile &) { // only change is to add "volatile"
/* Do nothing. */
}
Also, nice that you put the const
after the type, where it belongs.
The compiler warning has nothing to do with used or unused. You are passing a volatile variable - readWriteActivated - to a function which does not accept a volatile reference. Try a const cast.
It is a bug:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42655
There is no work around at the unused() level. Instead, each occurrence may be worked around by introducing a temporary variable before calling unused():
template<typename T>
void
unused(T const &) {
/* Do nothing. */
}
int main() {
volatile bool x = false;
bool avoidGCC42655 = !x; // type of "!x" is bool
unused(avoidGCC42655);
}