Boost logo

Boost :

Subject: Re: [boost] Heads up - string_ref landing
From: Yanchenko Maxim (maximyanchenko_at_[hidden])
Date: 2012-11-16 05:31:34


16.11.12, 13:37, "Olaf van der Spek" <ml_at_[hidden]>":
>
> On Fri, Nov 16, 2012 at 10:28 AM
> > Yes, it's cheap to calculate (end ptr is equally cheap to calculate btw) but you'll end up calculating it all the time in almost every function. At least my analysis showed that in real app size() is called much more frequently than end().
>
> Really? Do you use subscripted access then? With iterator access you'd
> use end().

Not only subscripted access. Taking a subrange also requires knowing size. Copying from/to (read memcpy) - same.
Filling (read memset) - same.
Comparing (read memcmp) - same.
(char_range is an optimization technique so we aim for maximum speed. If you don't maximize speed you'd be happy with simple and safe std::string copies.)
end() is needed only in operations at the end of the range or sequential operations on the range as a whole.
 
>
> > OTOH, storing only pointers is conceptually cleaner.
>
> You can derive size in bytes and memory range from a ptr pair without
> type info. That may be handy when debugging and in other situations.
> You can't do that with (ptr, size) without type info.

Sorry, I don't understand this, could you elaborate, please?

> >> Shouldn't they be implicit?
> >
> > Not from std::string. Same argument as for not having implicit conversion to char*.
>
> What argument would that be?

You are giving away a reference to string internals that are subject to change/die anytime. Making it explicit and visible in the caller code ensures that the programmer will take special measures to make sure that the string doesn't change/die while there's a char_range looking into it.

Consider std::vector<char_range>, for example.

For the same reason we have explicit char_range::literal and char_range::from_array.

Thanks,
Maxim


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