Boost logo

Boost :

Subject: Re: [boost] Iterator Range and operator==
From: Mostafa (mostafa_working_away_at_[hidden])
Date: 2012-04-21 20:42:26


On Fri, 20 Apr 2012 05:15:50 -0700, Olaf van der Spek <ml_at_[hidden]>
wrote:

> Hi,
>
> What do you expect this code to do? Is b true or false? And why?
> Is this expected behaviour?
>
> [1] #include <boost/range/iterator_range.hpp>
> [2] #include <string>
> [3][4] int main()
> [5] {
> [6] std::string s = "Olaf";
> [7] boost::iterator_range<std::string::iterator> r(s);
> [8] bool a = r == s;
> [9] bool b = r == "Olaf";
> [10] assert(a);
> [11] assert(b);
> [12] return 0;
> [14] }
>

I would expect both lines 8 and 9 to cause compilation failure. To
someone unfamiliar with the library, it's not /immediately/ clear what the
intent of either line is. If one wants to treat "s" as a range for the
purpose of comparison, then one should explicitly adapt it to such. Ditto
for line 9. It just makes the maintaining code that much easier. In the
case of line 9, this has the additional benefit of forcing newcomers to
ask what does make_iterator_range("Olaf") mean?, leading to a
documentation look up that will preempt any potential surprises associated
with char arrays and the library.

Another note on implicit conversions, if line 8 means implicitly adapt "s"
to "r"'s type and then does a comparison, then the following:

[*] bool c = (s == r);

by symmetry should mean adapt "r" to "s"'s type and then do a comparison,
which clearly isn't the current behaviour. Even if it was, then [*] and
[8] may then give different results when unicode comes into play. Which
is another reason in favor of explicit conversions for both [8] and [9].

Mostafa


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