|
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:
http://www.cs.unc.edu/~smithja/MIMS/DataModel/research/DBC.html
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.
http://www-und.ida.liu.se/~freku045/dbcpp/
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.
http://www.halcyon.com/www3/jesjones/Whisper/Description.html
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.
http://www.cbuildermag.com/features/2000/08/cb200008gm_f/cb200008gm_f.
asp
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
configurable.
http://www-
ctp.di.fct.unl.pt/~pg/Tools2000/Another_Mediocre_Assertion_Mechanism_f
or_C++.pdf
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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk