Boost logo

Boost :

From: John Torjo (john.lists_at_[hidden])
Date: 2003-08-07 03:41:21


Hi Michael,

Sorry for the late reply...

>
> A postcondition such as I'm suggesting would perform an assertion when a
> function (or the enclosing block) exits instead of on the line where the
> assertion was defined. Obviously, if a function only has one exit path, it
> would be simple just to put assertions at the end of the function, but if
a
> function has multiple exit points, this would be inadequate.
>
> An example of what I mean is something like this (where SMART_ASSERT_POST
is
> the new postcondition assertion that I'm suggesting):

I'm not sure we need this.
It's much simpler to have something like:

struct on_exit {
    template< class function>
    on_exit( function f) { ... }
    ~on_exit() { call function }
private:
 ...
};

and in code:
// something similar to
on_exit exit( mem_fun( &SomeClass::CheckClassInvariants) );

>
> class SomeClass(void)
> {
> public:
>
> void SomeFunction(void)
> {
> file://Precondition: check class invariants on function entry
> SMART_ASSERT(CheckClassInvariants());
>
> file://Postcondition: check class invariants on function exit
> SMART_ASSERT_POST(CheckClassInvariants());
>
> file://Function body here
> }
>
> private:
>
> bool CheckClassInvariants(void)
> {
> file://Return true if class invariants are met
> }
> };
>
> Another example would be something like this:
>
> int SomeFunction(int arg)
> {
> file://Precondition: check argument on function entry
> SMART_ASSERT(arg >0 && arg < 10);
>
> int result = 0;
>
> file://Postcondition: check result on function exit
> SMART_ASSERT_POST(ref(result) >= 0);
>

Using ..._POST could just complicate things.
Users should know to use, for each tested argument ref(...),
so you can come up with something like outlined above.

I'll think about it (something using lambda expressions).

Best,
John

> file://Function body here
>
> return result;
> }
>
> As far as how it would be implementated, as I suggested before, I assume
the
> post condition would create an object whose destructor asserts the
condition
> when the block the it is defined in exits.
>


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