Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-02-23 10:34:48


Christopher Diggins
Object Oriented Template Library (OOTL)
http://www.ootl.org
----- Original Message -----
From: "Douglas Gregor" <doug.gregor_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Wednesday, February 23, 2005 8:27 AM
Subject: Re: [boost] STL containers with contracts, any interest?

>
> On Feb 23, 2005, at 2:32 AM, christopher diggins wrote:
>
>> I am writing a set of contract verification classes for the various STL
>> containers. These are wrapper classes which verify DbC style contracts
>> (i.e. preconditions, postconditions and invariants). I have already
>> written one for std::vector you can see it at: http://www.ootl.org/pwc/
>>
>> Is anyone interested in seeing this submitted Boost, if I extend it to
>> cover the basic collections? e.g. list, map, set, multimap, multiset,
>> deque, queue, stack?
>
> Is there any advantage to this kind of markup vs. the "debug modes" that
> most standard libraries now have?
> The latest standard libraries from Metrowerks, Dinkumware, GCC, and
> STLport all have some form of debug mode that AFAICT actual do more
> checking (e.g., checking for uses of invalid iterators). Here's the
> reference for GCC's debug mode:
> http://gcc.gnu.org/onlinedocs/libstdc++/debug.html#safe
>
> Doug

Hi Doug,

1) The proposed PwC approach would provide consistent behaviour across STL
implementations.
2) The contract verification classes would make it much easier to write new
classes which model STL containers and verify that they exhibit correct
behaviour.

Also consider the following code:

template<Vector_T>
void MyAlgorithm(Vector_T& x) {
  // do stuff ...
}

If we get a hand-rolled vector type (instead of an STL vector) we have no
way of knowing whether or not it conforms to the expected contract even if
we can check it conforms to the correct concept. Using contract classes we
could instead write (using an imaginary vector_contract_concept):

template<Vector_T>
void MyAlgorithm(Vector_T& x) {
  #ifdef DEBUG
    BOOST_STATIC_ASSERT(vector_contract_concept<Vector_T>::satisfied)
  #endif
  // do stuff
}

I am not saying that this is the best usage of the contract classes, but it
is one possible way to use them. There are probably better examples, but I
haven't had my coffee yet ;-)

Btw thanks to Joaquín for pointing out the error in swap.

Christopher Diggins
http://www.ootl.org


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