Boost logo

Boost :

Subject: Re: [boost] [review] string convert
From: Matthew Chambers (matt.chambers42_at_[hidden])
Date: 2011-05-05 14:07:19


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)
>
> I removed the following from my earlier list because I think try_convert_to() is a suitable replacement:
>
> - optional<T> name_me_1<T,S>(S, formatting = none)
> - optional<T> name_me_1<T,S>(S, T, formatting = none)
> - pair<T,int> name_me_2<T,S>(S, T, formatting = none)
>
> (The second is not needed because of the default_value CP.)
>
> Clearly, try_convert_to() can be the foundation of the other function templates. Phil's suggestion was to forego the others and just provide the foundation. I'm not sure that's necessary, but it remains an option.
>
> 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.
>
> 1 is satisfied by T convert_cast<T,S>(S, formatting = none).
>
> 2 is satisfied by T convert_cast<T,S>(S, T, formatting = none).
>
> 3 is satisfied by try_convert_to(), though by virtue of having first created a T to pass as the second argument. name_me_1 would be a more direct way to satisfy this use case.
>
> 4 is satisfied by try_convert_to().

What would a use case look like for converter<T,S>? 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?

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

-Matt


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk