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
>>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
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));

// 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).

Anthony Liguori

Boost list run by bdawes at, gregod at, cpdaniel at, john at