Boost logo

Boost :

Subject: Re: [boost] [review] string convert
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-10 08:02:36


Matthew Chambers wrote:
> On 5/9/2011 4:10 PM, Stewart, Robert wrote:
> > Matthew Chambers wrote:
>
> >> I agree we need to support multiple manipulators, but yuck!
> >> The nicest way I can think of to do that is with separate
> >> parameters for each manipulator category:
> >>
> >> int i = convert::as<int>("+42", (_showpos=true,
> >> _base=ios::oct));
> >> string s = convert::as<string>(123.456, (_scientific=true));
> >> // or _float=ios::scientific ?
> >
> > Do those apply to the insertion into or extraction from the
> > stream?
>
> Using a single stringstream for a string<->non-string
> conversion, I'm not sure it makes a difference.

I suppose it wouldn't be too hard to document that any manipulators supplied in the conversion apply to the string side of the conversion and, thus, may be insertion or extraction manipulators, depending upon the target and source types.

> The only case I thought of where it would matter is a
> string<->string, i.e.
>
> "42"(dec)<->"2A"(hex)
> "123.456"(fixed)<->"1.23456e+2"(scientific)

Those wouldn't work as they would require understanding the string's content in order to select an intermediate type for the conversion or else a way to specify the intermediate type. That is, whereas lexical_cast does a source-to-stream-to-target conversion, what you've shown would require a string-to-stream-to-numeric-to-stream-to-string conversion and that requires specifying the intermediate numeric type. IOW, those would actually require two conversions:

double const d(convert_to<double>("42", format_ = std::dec));
std::string const s(
   convert_to<std::string>(d, format_ = std::hex));

So, std::dec would be applied for the extraction, while std::hex for the insertion, because the string type is the source in one case and the target in the other. That makes sense.

Vicente, you raised the issue. Is there a use case we missed that requires manipulators for both insertion and extraction, given that string<->string conversions can use manipulators because there's no intermediate conversion?

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

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 acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk