Boost logo

Boost :

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


Matthew Chambers wrote:
> On 5/5/2011 12:35 PM, Stewart, Robert wrote:
>
> > OK, that means we're down to the following now:
> >
> > - default_value<T> customization point
> > - converter<T,S> customization point, main logic, functor
> > - T convert_cast<T,S>(S, formatting = none); can throw
> > - T convert_cast<T,S>(S, T, formatting = none)
> > - bool try_convert_to<T,S>(S, T&, formatting = none)
> >
> > It's worth enumerating the use cases:
> >
> > 1. lexical_cast-style usage: new-style cast with exception
> > 2. new-style cast with fallback and no exceptions
> > 3. indicate whether the conversion succeeded; no value if
> > failed
> > 4. indicate whether the conversion succeeded; use fallback
> >
> > Is that a complete list? I suspect I'm missing something.
>
> What would a use case look like for converter<T,S>?

Oh, right. I didn't list the use cases for a function object interface to use with algorithms or to reuse a particular conversion configuration (some set of manipulators, etc.).

> Some lambda syntax was discussed. Say I want to convert a
> vector<string> to a vector<int>:
>
> vector<string> source;
> source.push_back("A1");
> source.push_back("foo");
> vector<int> target;
> transform(source.begin(), source.end(), back_inserter(target),
> converter<int>(formatting = hex));
>
> Would something like that work?

You need the source and target types, so I think it would be more like this:

transform(source.begin(), source.end(), back_inserter(target),
   converter<string,int>(format = hex));

> Perhaps another (cooler) use of this could be as an iterator
> and/or range adapter.
>
> vector<int> target;
> boost::copy(source | boost::adaptors::converter<int>(
> formatting = hex), back_inserter(target));

Interesting.

Again, you'd need to specify the source and target types.

It occurs to me that the name "converter" is a bit ambiguous WRT its two template parameters. Which should be first, source or target? Arguably, source then target makes sense, since that's the direction of the conversion, but the cast function templates list them in reverse order because the source type can be deduced while the target type must be specified. That could be confusing. converter_from_to<Source,Target> would help, though it's uglier.

_____
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