From: Goran on
On Jun 30, 2:44 pm, "Martin B." <0xCDCDC...(a)gmx.at> wrote:
> Jens Auer wrote:
> > I am trying to implement some tools to ease design by contract. To
> > check invariants automatically at the beginning and end of member
> > functions, I intend to use a helper object which does this in RAII-
> > fashion:
>
> > class InvariantViolation: public std::logic_error
> > {
>
> > };
>
> > class InvariantChecker
> > {
> > public:
> > InvariantChecker( boost::function< bool () > const& predicate):
> > mPredicate( predicate )
> > {
> > _check();
> > }
>
> > ~InvariantChecker()
> > {
> > _check(); // may throw! What else to do?
> > }
>
> I've never used uncaught_exception() myself, but if you wrap the
> _check() in if(!uncaught_exception()) then you can handle the case where
> your dtor is called for stack-unwind in case of exception. Or so I
> guess. :-)

That seems like a great idea! ( But, after using uncaught_exception()
in 1-2 places, all I can say - it worked for me ;-) ).

The requirement, then, I suppose, is that if you're exiting with
exception, invariants aren't relevant. But that's possibly not good
enough. E.g. method has strong exception safety requirement - if
that's to be checked, _check() must be run even in uncaught_exception
case.

:-(

Goran.


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]