Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] char types and UDTs
From: Antony Polukhin (antoshkka_at_[hidden])
Date: 2012-04-11 00:22:05


2012/4/11 Eric Niebler <eric_at_[hidden]>:
> There is an internal trait, boost::detail::stream_char, that
> lexical_cast uses to determine what the character type of the source is
> (if any). It's specialized on only a fixed number of types. Since it's
> an implementation detail, it can't be used to extend the set of types.
> Also, I tried specializing it. In past versions of Boost that just
> worked, but it doesn't anymore.
>
> I think being able to extend lexical_cast to support types like
> (std|boost)::wssub_match is an essential feature. Thoughts on how to get
> there from here?

Specializing boost::detail::stream_char is not nice. Why just you
don't specialize lexical_cast directly:

namespace boost {

template <class Target, class BiIter>
Target lexical_cast(const sub_match<BiIter>& m)
{
   return boost::lexical_cast<Target>(m.str());
}

} // namespace boost

[Not tested]

Using this approach you will also get a faster version of lexical_cast
(which does not copy data to STL stream and does not construct heavy
STL stream objects). This approach is also described in lexical_cast
trunk documentation, in section 'Tuning classes for fast lexical
conversions'.

I'll add your question to the FAQ section of lexical_cast documentation.

--
Best regards,
Antony Polukhin

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