Boost logo

Boost :

From: Roland Schwarz (roland.schwarz_at_[hidden])
Date: 2007-10-29 11:15:54


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


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk