Boost logo

Boost :

Subject: [boost] [lexical_cast] char types and UDTs
From: Eric Niebler (eric_at_[hidden])
Date: 2012-04-10 22:50:22


Say I have a wide-character, user-defined, streamable type like
boost::wssub_match from the regex library (or now std::wssub_match).
It's basically a pair of wstring iterators. It's stream insertion
operator is defined like this:

template <class charT, class ST, class BiIter>
basic_ostream<charT, ST>&
operator<<(basic_ostream<charT, ST>& os, const sub_match<BiIter>& m)
{
    return (os << m.str());
}

If it's not obvious, it requires that the stream's character type is the
same as the BiIter's value_type. If I were to try to use
boost::lexical_cast to cast this to something, it won't work. That's
because lexical_cast doesn't know about wssub_match, and so doesn't know
that its character type should be wchar_t. Instead, it defaults to char
for any unknown types, and doesn't give a way to override this.

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?

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