Boost logo

Boost :

Subject: Re: [boost] [review] string convert
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-06 14:34:40

Vicente BOTET wrote:
> lexical cast hides the stream usage, but we have
> ios << source;
> ios >> target;
> If we want to support type-to-type via stream and manimulators
> we will need to be able to do in the implementation
> ios << omn_1 << ... << omnp_n << source;
> ios >> imnp_1 >> ... >> imnp_k >> target;
> As far as I perceive the Boost.Convert library the manipulators
> are used either to convert to a string or from a string. I have
> not see deeply hwat allows the converter class. Can someone
> tell me if the reviewed interface can be used to make such
> type-to-type conversions?

It is one way and, you're right, that's an issue.

> We could also define an operation that takes the stream and
> extract a type returning the value instead of requesting a
> variable
> int i = extract(str >> ios >> std::hex)

OK. To make str >> ios work requires something like this:

   convert::iostream &
   operator >> (std::string const &, convert::iostream &);

> string str = extract(ios << std::hex << i);

OK, that works if convert::iostream holds a std::string, or wraps a std::ostringstream, which extract can access.

> With this interface we can add in a single expression output
> manipulators and input manipulators.

That offers convenience and a lot of flexibility. Furthermore, convert_cast, try_converting_to*, etc. can be specified as using the convert::iostream mechanism, by default, relying on default stream formatting. That would provide a convenient, well understood fallback mechanism while leaving room for customized conversions for particular types.

More concretely, the non-stream interfaces can use a converter class to effect the conversions. That class can have a primary specialization that uses convert::iostream to effect the conversion with no special formatting, thereby defaulting to using the IOStreams-based insertion and extraction operators for the source and target types. Specializations of the converter class can provide optimized conversions for particular types or type pairs. Then, if that converter class were also the functor type, there would be a single point of customization for all supported conversions, stream-based and not.

Rob Stewart robert.stewart_at_[hidden]
Software Engineer using std::disclaimer;
Dev Tools & Components
Susquehanna International Group, LLP

* "try_converting_to" is a better name than "try_convert_to."

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.

Boost list run by bdawes at, gregod at, cpdaniel at, john at