Boost logo

Boost :

From: Noah Stein (Noah_at_[hidden])
Date: 2002-10-31 03:13:12


> Martin:

> Q: how many of you people are using programming by contract
> already in C++?

I can't answer that. I can say that sometime within the last year, there
was discussion in this very mail list about the topic. I know that there's
at least a small population talking about it and probably trying to use it
in some form.

> Q: do you think there is no practical solution in C++?

I've used DBC somewhat effectively with curiously-recursive template
classes. It could also be rather effective with abstract base classes. I
think the lack of a final like Java has can reduce the effectiveness,
though. Also, invariant checking must be explicitly called for every
non-const member function. Invariants would also not run when public member
variables are changed.

> Here is a prototype (not a template yet) that illustrates one way that
> programming by contract could be imlemented. In this example the pre/post
> condition functions get executed in the order that they are
> declared in the
> myContract class. I am looking for ways to automate writing of such code
> possibly using templates. Here is the prototype and execution code:

Why is the class necessary in your example? For pre-conditions and
post-conditions, why does DBC require much beyond assertions? Admittedly,
Eiffel pre-conditions and post-conditions can get quite complex, and I think
it's good to support that in C++. Most of my functions don't need much
beyond simple assertion mechanics. For those that do, you can always write
a private const member function to ensure that you are not modifying what
you are testing. In the case of free functions, you could package up the
data into a tuple of const items and pass that to pre- & post-condition
checking functions. Am I missing something?

-- Noah


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