Boost logo

Boost :

Subject: Re: [boost] [assert] static_assert envy
From: Patrick Horgan (phorgan1_at_[hidden])
Date: 2011-01-18 18:19:07


On 01/18/2011 09:47 AM, Robert Kawulak wrote:
> 2011/1/18 Steven Watanabe<watanabesj_at_[hidden]>:
>> What exactly is wrong with
>> BOOST_ASSERT( !foo() || assertion )
>> or
>> BOOST_ASSERT( foo() ? assertion : true )
>>
>> IMHO, the last thing we need is lots of
>> minor variations that don't add any
>> real utility.
> The utility here is communicativeness. Upon seeing:
>
> BOOST_ASSERT( !"we shouldn't get there" );
> BOOST_ASSERT( condition&& "this shouldn't happen" );
> BOOST_ASSERT( !foo() || condition&& "something is wrong" );
>
> you have to turn your mind into a Karnaugh table for a while if you
> want to understand what this code does. OTOH:
>
> BOOST_FAIL_MSG( "we shouldn't get there" );
> BOOST_ASSERT_MSG( condition, "this shouldn't happen" );
> BOOST_ASSERT_IF_MSG( foo(), condition, "something is wrong" );
>
> communicates the author's intention in a direct way and also makes it
> harder to make mistakes in the boolean expressions (note that foo and
> condition above may be boolean expressions themselves making the first
> technique quite error-prone).
I find these exactly as prone to confusion and mistakes as the other.
In each you have to stop for a minute and figure out what they mean,
except with the first, it's normal boolean logic that you have to
understand each day to program in C or C++. With the latter you have to
first check your understanding of the idiom, understand what it implies
and then slot each part into the resultant boolean expression. You end
up at the same mental process either way.

I'm not saying that I'm against it, although I'd probably go with the
naked assert as being clearer to any C/C++ programmer. Then they
wouldn't have to learn another thing in order to be able to understand
what I was talking about. I usually go as close to the basic language
as possible for just that clarity. But that's just my style. If
someone else wants to obfuscate a bit I don't mind it.

> You could equally well ask what is the real utility of having
> C++-style casts over the C-style one. ;-)
It's a bad analogy. The BOOST_FAIL_MSG and it's ilk just overlays
another level of indirection. It adds more complexity. C++ style casts
reduce complexity.

Best regards,

Patrick


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