Boost logo

Boost :

From: williamkempf_at_[hidden]
Date: 2001-04-16 15:39:34

I've been researching the web for articles on DbC with C++ to come up
with ideas for Boost.Assertion. This is what I've found and comments
I have on each site:

A general discussion about DbC with references to implementation
languages and tools. If you're unfamiliar with DbC this is a good
place to start.

This is a pre-processing approach to adding DbC to C++. This
probably won't be of much help when designing an assertions library
but is worth looking at any way.

This is a complete C++ GUI framework with the only interesting part
for this discussion being three macros, PRECONDITION, POSTCONDITION
and CHECK_INVARIANT. The page makes this sound like pretty much what
I'm trying to do with Boost.Assertion but I've not looked into the
code to evaluate it yet.

Talks about DbC as it applies to C++. Code is provided to add DbC
style assertions to C++ code. For the most part the C++ preprocessor
is not used, with inline methods being used for assertions. This is
an interesting approach. I see only two possible problems. First,
I'm not 100% sure the optimizer can reduce such assertions to no-ops
when assertions are turned off. At the very least the expression is
going to be evaluated with this approach. Second, this prevents us
from using the __FILE__ and __LINE__ macros effectively. Inheritance
is not addressed at all in this article. The assertions are also not


This paper defines a complete (well, as near as can be done in C++)
DbC system in C++. Pre/post-conditions, class invariants, loop
invariants and checks included. Inheritance is addressed, at least
as much as is possible with out compiler support of DbC, and
unfortunately it requires some discipline to get right. This library
also avoids the preprocessor, with the same drawbacks I listed
above. All assertion functionality is found in an Assertions class.
To declare invariants one must inherit their class from Assertions
and override a virtual invariant() method. This is needlessly
intrusive on class heirarchies. There's also a problem with
invariants being checked too often since ensure() checks the
invariant with each call. Selectively enabling levels of assertions
is mentioned, but not addressed. The ability to check "old" values
in postconditions is addressed, but only for int types. The paper
mentions using a "polymorphic map", but no help is given in
implementing such a concept. It would likely be easier to make use
of the C++ preprocessor to acheive the same effect. The library also
includes loop variants and invariants, something I have not included
in Boost.Assertions.

I'd love to hear other's comments or to hear about other references
on this topic.

Bill Kempf

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