Boost logo

Boost :

From: christopher diggins (cdiggins_at_[hidden])
Date: 2005-05-31 23:46:20


"Jonathan Turkanis" <technews_at_[hidden]> wrote in message
news:d7j8it$1pt$1_at_sea.gmane.org...
> christopher diggins wrote:
>
>> violates the container postconditions. Just for example consider the
>> following common idiom:
>>
>> template<class Container_T>
>> void some_very_dangerous_code(Container_T& x) {
>> x = Container_T();
>> if (x.empty()} {
>> SayHelloToAlphaCentauri();
>> }
>> else {
>> SendNukesToAlphaCentauri();
>> }
>> }
>>
>> Obviously this will not work as expected.
>
> What's expected here?

What was expected was that the SendNukesToAlphaCentauri() control path
should never be executed BUT I made a big mistake (and a common one at
that). I assumed x == y implied x.size() == y.size() for standard
containers. That is not in fact an explicit postcondition of containers as
far as I know. Since the example is unsatisfactory here is another:

template<class Container_T>
void some_very_dangerous_code() {
  Container_T x();
  if (x.empty()} {
   SayHelloToAlphaCentauri();
  }
  else {
   SendNukesToAlphaCentauri();
  }
}

The point is not whether or not this is a good example but rather that
incorrect and unexpected (possibly disastrous) behaviour will be exhibited
in existing code by using a non-conformant container in a context where a
conformant container is expected.

-- 
Christopher Diggins
http://www.cdiggins.com 

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