Boost logo

Boost :

Subject: Re: [boost] [assert] static_assert envy
From: Thomas Klimpel (Thomas.Klimpel_at_[hidden])
Date: 2011-01-18 16:56:20


vicente.botet wrote:
> If we want to evaluate a condition when BOOST_DISABLE_ASSERTS is not
> defined we could add a macro BOOST_WHEN that can be used like
>
> BOOST_WHEN( !c1.empty() && !c2.empty()) {
> BOOST_ASSERT_MSG(c1.front() ? c2.front() : def_val(), "something is
> wrong" );
> }
>
> and defined as
>
> #if defined(BOOST_DISABLE_ASSERTS)
> # define BOOST_WHEN( CND) if(true) {} else
> #else
> # define BOOST_WHEN( CND) if(!(CND)) {} else
> #endif

That's an interesting technique. I wondered before how Boost.Contract could achieve its tricks, but it probably doesn't even use this technique. However, wouldn't it be simpler to define

#if defined(BOOST_DISABLE_ASSERTS)
# define BOOST_ASSERT_CODE if(true) {} else
#else
# define BOOST_ASSERT_CODE if(false) {} else
#endif

and then write

BOOST_ASSERT_CODE {
   if (!c1.empty() && !c2.empty()) {
      BOOST_ASSERT_MSG(c1.front() ? c2.front() : def_val(), "something is wrong" );
   }
}

If a construct like this would be part of a Boost.Contract library, I certainly wouldn't object to it during a review. I'm not so sure whether I like it enough just of its own. But I certainly prefer it over BOOST_ASSERT_IF_MSG, because now I can write my assertion code in a language I already know, and it's just a single macro (more or less, we still have BOOST_DISABLE_ASSERTS and BOOST_ASSERT_MSG and all the related stuff).

Regards,
Thomas


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