Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] char types and UDTs
From: Eric Niebler (eric_at_[hidden])
Date: 2012-04-11 11:18:06


On 4/11/2012 1:13 AM, Olaf van der Spek wrote:
> On Wed, Apr 11, 2012 at 8:54 AM, Eric Niebler <eric_at_[hidden]> wrote:
>> Calling m.str() creates a temporary std::wstring object, which incurs a
>> dynamic allocation and is slow. Speaking for my own library (xpressive),
>> sub_match has an optimized stream insertion operator. It should be used.
>
> You have to construct a string somewhere, don't you?
> If you have one already, you could use iterator_range<const char*>
> instead to avoid a copy.

Yes, I see that lexical_cast has optimizations for
iterator_range<wchar_t const *> and a few other, sufficiently
'string-like' types. But sub_match essentially *is* a string-like
iterator_range. (It's a std::pair of iterators.) I'm genuinely surprised
there's no way to tell lexical_cast that. Instead I have to just know
(a) which are the magical types lexical_cast is optimized for and (b)
for which it can determine the correct underlying stream character type
(hint: the docs are unclear or out of date), and massage my type into
one of those before calling lexical_cast. Why?

>> But my primary objection is below...
>
> You're right, it'd be nice if that just worked.

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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