Boost logo

Boost :

Subject: Re: [boost] Iterator Range and operator==
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2012-04-23 10:31:37


On 22/04/12 02:42, Mostafa wrote:
> 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.

There is absolutely no reason to, this code makes perfect sense.
iterator_range has an overloaded operator== that allows it to compare it
to any other range.

> To
> someone unfamiliar with the library, it's not /immediately/ clear what
> the intent of either line is.

It is immediately clear once you know what types are ranges and what
operator== on iterator_range does.

> If one wants to treat "s" as a range

s is a std::string. It is already a range. There is no need to "treat
it" as such.

> 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].

I have no idea what you're talking about.

There is no conversion involved.

s == r just does std::equal(begin(s), end(s), begin(r))


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