BOOST_ASSERT_DECL to get rid of "unused variable" warnings

Library authors often use the pattern: void foo(void) { unsigned res = SomeFuncWithSideEffects(); BOOST_ASSERT(res); } This causes a lot of noise on some compilers (e.g. gcc) about unused variables when building in release mode. I suggest a macro void foo(void) { BOOST_ASSERT_DEF( unsigned res = ) SomeFuncWithSideEffects(); BOOST_ASSERT(res); } to get rid of these warnings, in cases where the variable is only used for assertion. In boostroot/boost/assert.hpp add #if defined(BOOST_DISABLE_ASSERTS) # define BOOST_ASSERT_DEF( tokens ) #elif defined(BOOST_ENABLE_ASSERT_HANDLER) # define BOOST_ASSERT_DEF( tokens ) tokens #else # if defined(NDEBUG) # define BOOST_ASSERT_DEF( tokens ) # else # define BOOST_ASSERT_DEF( tokens ) tokens # endif #endif Comments welcome! In particular if anyone thinks that the above pattern can or should better be avoided at all, I would be glad to learn how this can be done. Roland aka speedsnail

From: Roland Schwarz
Library authors often use the pattern:
void foo(void) { unsigned res = SomeFuncWithSideEffects(); BOOST_ASSERT(res); }
This causes a lot of noise on some compilers (e.g. gcc) about unused variables when building in release mode.
I suggest a macro
void foo(void) { BOOST_ASSERT_DEF( unsigned res = ) SomeFuncWithSideEffects(); BOOST_ASSERT(res); }
MFC had (has) a VERIFY macro for this. If we introduced a BOOST_VERIFY, the usage would look like: void foo(void) { BOOST_VERIFY( SomeFuncWithSideEffects() ); } Implementation would be (something like): #if defined(NDEBUG) # define BOOST_VERIFY( expression ) expression #else # define BOOST_VERIFY( expression ) bool b = expression; BOOST_ASSERT(b) #endif (Yes, I know; real code would need to invent a unique name that wouldn't clash, or introduce a scope, or something, but you get the idea.) -- Martin Bonner Senior Software Engineer/Team Leader PI SHURLOK LTD Telephone: +44 1223 441434 / 203894 (direct) Fax: +44 1223 203999 Email: martin.bonner@pi-shurlok.com www.pi-shurlok.com disclaimer

Martin Bonner wrote:
MFC had (has) a VERIFY macro for this. If we introduced a BOOST_VERIFY, the usage would look like: void foo(void) { BOOST_VERIFY( SomeFuncWithSideEffects() ); }
This wouldn't account for: unsigned int res = SomeFuncWithSideEffects(); BOOST_ASSERT(res != EDOM); or the like... Roland aka speedsnail

AMDG Roland Schwarz <roland.schwarz <at> chello.at> writes:
This wouldn't account for:
unsigned int res = SomeFuncWithSideEffects();
BOOST_ASSERT(res != EDOM);
or the like...
Roland aka speedsnail
template<class T> void use_variable(const T&) {} #if ENABLE_ASSERT #define BOOST_ASSERT(x) normal implementation #else #define BOOST_ASSERT(x) use_variable(x) #endif In Christ, Steven Watanabe

Steven Watanabe wrote:
template<class T> void use_variable(const T&) {}
#if ENABLE_ASSERT #define BOOST_ASSERT(x) normal implementation #else #define BOOST_ASSERT(x) use_variable(x) #endif
Looks good to me :-) @Peter: You are listed as the author of assert.hpp, do you think this could be done? Or will this disable the compilers optimizer, so he can do away with the return value? Roland aka speedsnail

Steven Watanabe wrote:
template<class T> void use_variable(const T&) {}
#if ENABLE_ASSERT #define BOOST_ASSERT(x) normal implementation #else #define BOOST_ASSERT(x) use_variable(x) #endif
Looks good to me :-)
@Peter: You are listed as the author of assert.hpp, do you think this could be done? Or will this disable the compilers optimizer, so he can do away with the return value?
Roland aka speedsnail
This doesn't seem to do what you want. BOOST_ASSERT( this_takes_time() ); // breaking change The introduction of BOOST_VERIFY with a similar implementation would probably be best. #if ENABLE_ASSERT #define BOOST_VERIFY(x) BOOST_ASSERT(x) #else #define BOOST_VERIFY(x) ((void)(x)) #endif

Peter Dimov wrote:
This doesn't seem to do what you want.
BOOST_ASSERT( this_takes_time() ); // breaking change
I already recognized it :-(
The introduction of BOOST_VERIFY with a similar implementation would
What is with BOOST_VERIFY(EDOM != Func()) Will this work as expected? Roland aka speedsnail

Peter Dimov wrote:
The introduction of BOOST_VERIFY with a similar implementation would
What is with BOOST_VERIFY(EDOM != Func())
Will this work as expected?
Roland aka speedsnail
I think that it will. You can try it out and see if it matches your expectations: #include <boost/assert.hpp> #if defined(BOOST_DISABLE_ASSERTS) || ( !defined(BOOST_ENABLE_ASSERT_HANDLER) && defined(NDEBUG) ) #define BOOST_VERIFY( x ) ((void)(x)) #else #define BOOST_VERIFY( x ) BOOST_ASSERT( x ) #endif #include <iostream> #include <cerrno> int f() { std::cout << "f()\n"; return 0; } int main() { BOOST_VERIFY( EDOM != f() ); }

Peter Dimov wrote:
I think that it will. You can try it out and see if it matches your expectations:
Yes, it does! Would you possibly please add this macro to assert.hpp for general availability in boost libs? Thank you, Roland aka speedsnail
participants (4)
-
Martin Bonner
-
Peter Dimov
-
Roland Schwarz
-
Steven Watanabe