Boost logo

Boost :

From: Eric Woodruff (Eric.Woodruff_at_[hidden])
Date: 2002-11-11 11:55:36


I've always used the examples out of TC++PL of some assertions:

template <typename Exception>
assertion (bool const condition) {
     if (!condition) {
        throw Exception ();
    }
}

template <typename Exception>
assertion (bool const condition, Exception const& exception) {
    if (!condition) {
        throw exception;
    }
}

What about having a function-class based assertion that worked like:

assertion (!Sanity::invalidArgument || 0 != p, Throw (invalid_argument
("must not be zero")));

assertion (2 > x, Abort ());

or maybe even

assertion (2 > x, DontCompile ());

---- implementation ---

template <typename Function>
assertion (bool const condition, Function const& f) {
    if (!condition) {
        f ();
    }
}

It looks very idiomatic to me.

I've also found things like this useful:

template <typename Condition, typename Exception>
Condition const& assertNonZero (Condition const& condition) {
    if (0 == condition) {
        throw Exception ();
    }

    return condition;
}

Consider:

return x / assertNonZero<range_error> (b - a);

"Kevin S. Van Horn" <Kevin.VanHorn_at_[hidden]> wrote in message
news:Pine.LNX.4.44.0211110833530.25298-100000_at_speech.cs.ndsu.nodak.edu...
> It's been six days since I posted this, without a single response, so I'm
> going to try again. Based on earlier discussions, I thought there might
> be some interest in this. Does anyone have any problems with the proposed
> interface? Should I turn this into a formal proposal for submission to
> Boost? Peter, how does this compare with the changes to
> <boost/assert.hpp> you were planning to do / are doing?
>


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