tags:

views:

305

answers:

10
A: 

Why not skip templates altogether and go with the ellipses.

inline void unused (...) { /* do nothing */ }
eduffy
Notice this will not work with non-PODs. Violates #4
Johannes Schaub - litb
eduffy
Variadic functions must have at least one non-variadic parameter.
Roger Pate
A: 

Why are you passing !readWriteActivated instead of readWriteActivated if the value of the expression doesn't matter?

Nicolás
I am not sure why this was down voted.. It is infact the appropriate question.
Jagannath
No the testcase is simplified. In the real code the negation is needed because it is an assertion and remove the negation would give the wrong assertion.
WilliamKF
@WilliamKF: if it is an assertion, then you should post code that demonstrates the usage you intend (are you trying to produce something like assert?)
Evan Teran
I was trying to simplify the test case down to something small. The assertion looks like:#ifdef NDEBUG# define Assert(expression) unused(expression)#else // not NDEBUG# define Assert(expression) \{ \ bool test = (expression); \ \ if (!test) { \ if (StopHere(__LINE__, __FILE__, __PRETTY_FUNCTION__, \ #expression, false)) { \ throw Exit(-1); /* So that destructors are run. */ \ } \ } \}#endif // else not NDEBUG
WilliamKF
+3  A: 

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.

jalf
This is not correct. Still we get the warning. This code is only partly effective. It does not silence the warning on all compilers.
Jagannath
@Jagannath: I think that testing on a beta compiler isn't exactly fair game since you've said that VC2010 is the culprit. Try it on just about every other compiler that is actually...released.
Evan Teran
@Evan: Agreed. Will try on other compilers and see.
Jagannath
+1  A: 

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.

Jagannath
don't name the parameter or you'll get a warning that "ignore" is unused :-P.
Evan Teran
Because the real code has an assertion of !readWriteActivated, in the optimized version the assertion goes away, but I don't want an unused variable warning when the assertion is the only usage of a variable.
WilliamKF
+1  A: 

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.

Evan Teran
@Evan, did you complile this? I get the warning on VC2010.
Jagannath
@Jagannath: I've never used VC2010, but casting to void is supposed to be the correct solution and has worked on just about every compiler I've used before.
Evan Teran
Shouldn't that be ((void)ignore) in the #else clause?
WilliamKF
If you want just a plain old `assert` style test macro, then no. But if you like, you can do as you suggest and it'll work just fine. Like i just edit'ed in, I would tackled to the unused and assertion issues completely separately which avoids the whole problem.
Evan Teran
+1  A: 

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.)

Roger Pate
This will not work in general, for example, what if the argument to the assert cannot be cast to bool, hence the template.
WilliamKF
You *already require this property* with your current macro, with `bool test = (expression);`.
Roger Pate
+2  A: 

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).

Dan Olson
+1  A: 

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)

Old answer (but still not a bad idea)

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.

Potatoswatter
WilliamKF
Where const belongs is a matter of opinion. The code is actually fine with or without any qualifiers; the error indicates a compiler bug.
Potatoswatter
In my version of the compiler, v3.4.6, adding the cast to bool around the argument to unused() does not work around the bug.
WilliamKF
Wow, that really sucks, and is pretty surprising. GCC 4 has been out for a while, though; maybe time to upgrade? I guess the next suggestion would be to cast to `int`, since in the bugzilla discussion they said the problem only affects `bool`.
Potatoswatter
+1  A: 

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.

keraba
A: 

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);
}
WilliamKF