|
Boost : |
Subject: Re: [boost] [review] string convert
From: Vicente BOTET (vicente.botet_at_[hidden])
Date: 2011-05-10 13:50:42
> Message du 10/05/11 14:03
> De : "Stewart, Robert"
> A : "'boost_at_[hidden]'"
> Copie à :
> Objet : Re: [boost] [review] string convert
>
> Matthew Chambers wrote:
> > On 5/9/2011 4:10 PM, Stewart, Robert wrote:
> > > Matthew Chambers wrote:
> >
> > > 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("42", format_ = std::dec));
> std::string const s(
> convert_to(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?
I've no example at hand, but I'm sure we can find type-to-type conversions that need either to use the manipulators for insertion or for extraction. If for string to type it is clear that the manipulators were applied to for extraction and that for type to string the manipulators are applied for insertion, it is not clear when these manipulators should be applied for type-to-type conversions.
Best,
Vicente
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk