Boost logo

Boost :

Subject: Re: [boost] Heads up - string_ref landing
From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2012-11-27 07:21:01


On Tue, Nov 27, 2012 at 3:42 PM, Daniel James <dnljms_at_[hidden]> wrote:
> On 26 November 2012 14:13, Andrey Semashev <andrey.semashev_at_[hidden]> wrote:
>> On Mon, Nov 26, 2012 at 5:48 PM, Daniel James <dnljms_at_[hidden]> wrote:
>>>
>>> A string isn't the same thing as a range of characters.
>>
>> Why?
>
> Strings are one of the most important types in programming, and
> they're usually handled differently from ranges. A string is
> essentially a thing, not a sequence of things. Using a range confuses
> the string's representation for its type.

I guess, that depends on the perspective. I imagine strings as
sequences of characters and typically my string processing code is
similar to that of other ranges. You just have a certain additional
knowledge of its elements' nature.

> A range of characters can be
> binary data, or a collection of small integers, I don't want them
> coming anywhere near string handling code, and in C++ the type system
> is the best way to handle that. It should be easy to have distinct
> overloads for strings and ranges.

Probably, although I can hardly imagine overloads of a single method
that have different semantics for a string and a range of characters.
This would likely be the sign of a poor interface. OTOH, interfaces
that just want to accept "some string" are common.

>>> That's why I suggested a customisation mechanism. Something would
>>> allow you to indicate that a third party type is a string and, if
>>> necessary, how to get a string_ref from it. Perhaps an ADL hook, or a
>>> template class that is specialized for strings, or something else
>>> entirely.
>>
>> That would mean that the range is limited to strings only.
>
> Not at all, you could still explicitly convert to a string_ref.

I would have been happy with what you suggest if I was sure this
customization mechanism is going to be adopted by third-party strings.
I have a good faith in begin()/end() adoption, at least because this
mechanism is used by the language core (range-based for) and it is
logical for strings to support it. I am not so sure for a type trait
or some other mechanism that detect string types.


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