Boost logo

Boost :

From: Tanton Gibbs (thgibbs_at_[hidden])
Date: 2003-04-26 17:19:10


>From: "David Abrahams" <dave_at_[hidden]>
> "Edward Diener" <eddielee_at_[hidden]> writes:
>
> >> I browsed the article (I confess to not having read everything, so
> >> please correct any misapprehensions). My sense is that the technique
> >> is oriented towards detecting programmer errors and responding via an
> >> exception.
> >
> > I don't think ENFORCE is oriented toward that at all. There's no
orientation
> > involved other than to throw an exception based on a condition. I agree
with
> > you that direct programming errors should generally not throw exceptions
but
> > should ASSERT so that the programmer can fix the error. However I don't
> > think ENFORCE has anything to do with this debate about when to ASSERT
and
> > when to throw exceptions. Perhaps the examples give the impression which
you
> > have
>
> I think so.
>
> > but I think the problem is just choosing better examples in which
> > one would want to throw an exception and not a technical issue as to
> > the benefits of using ENFORCE in order to simplify exception
> > throwing.
>
> Can you show me a better example? This is not a challenge. Really,
> if this ENFORCE idea is a useful one I want to understand it.

I think something like:

template<class T>
inline T& Enforce( T& obj, const char* msg ) {
    assert( !!obj, msg );
    return obj;
}

(also it's const equivalent...not shown)

now you can do the same sort of thing as mentioned in the article

Enforce(cout) << Enforce(MakeWidget())->ToString();

I would think that the compiler could optimize away the Enforce calls with
NDEBUG defined, but when not defined it will catch NULL derefs as the author
intended.


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