Boost logo

Boost :

From: Anthony Liguori (anthony_at_[hidden])
Date: 2003-04-28 00:33:43


Andrei Alexandrescu wrote:

>"Tanton Gibbs" <thgibbs_at_[hidden]> wrote in message
>news:008301c30c42$13306760$7bdd550a_at_Corp.Acxiom.net...
>
>
>>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.
>>
>>
Couldn't this be implemented with an extension to pre-existing smart
pointer classes? For instance, something like:

struct ThrowWidget {
   void operator()(Widget &) { throw
std::runtime_exception("Dereferencing NULL widget"); }
};

boost::checked_pointer<Widget> widget = MakeWidget();

widget.set_precondition(std::bind1st(std::not_equal_to<Widget>(), NULL));
widget.on_error(ThrowWidget());

// assume cout already has a precondition
cout << widget->ToString();

This approach allows one to always enforce an error checking policy on
an object's usage. It would be nicer if one could customize the errors
not only with state but with operations being performed but that would
probably be messy.

I've always wanted to play with the idea of passive error checking (I
liken it to polling verses callback paradigms with I/O).

Regards,
Anthony Liguori


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