Subject: Re: [boost] [review] string convert
From: Barend Gehrels (barend_at_[hidden])
Date: 2011-05-04 09:46:46
> So, here's something complete (?) for people to rip apart:
> int i = convert_cast<int>(s); // default behavior: might throw
> optional<int> i = convert_cast<int>(s, use_optional_instead_of_throwing_);
> int i = convert_cast<int>(s, 17); // won't throw; can't tell if conversion successful
> int i = convert_cast<int>(s, 17, throw_even_though_i_specified_a_failback_);
> optional<int> i = convert_cast<int>(s, 17, use_optional_instead_of_throwing_);
> pair<bool,int> i = convert_cast<int>(s, 17, fallback_and_report_success_);
> I am not concerned with names, just with syntax. So I'm using "convert_cast" although I actually don't care what it's called, and I have some very ugly very descriptive tag values with distinct types to trigger different overloads, which might be abbreviated dont_throw_, do_throw_, and, um, tell_fallback_ or something.
> All would also accept extra manip_/format_= and locale_ arguments as in Vladimir's proposal, except they have to be appended to the first parameter list because of the "no proxies" requirement.
> Doesn't this now cover all the functionality we've been talking about?
> Since there are now a bunch of overloads with different behavior, and no converter<> object, I figure these would dispatch to a customization point function that looks a lot like Vicente's, but probably with a default an extra bool parameter indicating whether to throw.
int i = convert_cast<int>(s); // agreed
optional<int> i = convert_cast<optional<int> >(s); // so optional
indicated in template parameter as return type - as always
int i = convert_cast<int>(s, 17); // agreed
int i = convert_cast<int, throw_even_though_i_specified_a_failback_>(s,
17); // template parameter as optional one there
optional<int> i ... (similar as other one with optional)
pair<bool, int> i = convert_cast<pair<bool, int> >(s, 17); // pair
indicated in template parameter
This would never need any runtime parameter -> dispatching possible on
compile-time -> no if's or cases necessary in implementation.
And it always returns the type is in the (first) template parameter of
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk