Boost logo

Boost :

Subject: Re: [boost] Boost.Algorithm design question
From: Nevin Liber (nevin_at_[hidden])
Date: 2011-11-02 14:44:53


On 2 November 2011 12:54, Olaf van der Spek <ml_at_[hidden]> wrote:

>> assert(s == s.c_str());
>> assert(std::string(p) == p);
>> assert(std::string(l, sizeof(l) - 1) == l);
>
> For what values does this fail?

assert(s == s.c_str()); fails when s contains an embedded 0.

assert(std::string(p) == p); invokes undefined behavior when p is 0 or
p points to something that isn't 0-terminated.

assert(std::string(l, sizeof(l) - 1) == l) fails when l contains an embedded 0.

> Isn't the operator== with std::string
> and const char* well defined?

It is well defined given the precondition that p points to something
that is 0-terminated. However, that precondition alone is not
sufficient to make it an equivalence relation.

In these case the equivalence relation is not a property of the types
but of the values they represent, no matter how much handwaving goes
on around here about how those cases aren't important. They can be a
property of the types involved, but they just aren't in this
circumstance, as const char* can mean too many different things.

-- 
 Nevin ":-)" Liber  <mailto:nevin_at_[hidden]>  (847) 691-1404

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