|
Boost : |
Subject: Re: [boost] [review] string convert
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-04 16:00:33
Vicente BOTET wrote:
> De : "Stewart, Robert"
> > Matthew Chambers wrote:
> > > On 5/4/2011 11:05 AM, Vicente BOTET wrote:
> > >
> > > > I agree that when a fail-back is given the user is not
> > > > interested in knowing if the conversion succeeded or not,
> > > > so in this case the return value should be T and not
> > > > optional T.
> > > > The question now is what function should be used,
> > > > convert_cast or try_convert_cast. As the function doesn't
> > > > throw I will use try_convert_cast, but as the function
> > > > returns type T I will use convert_cast.
> > >
> > > I disagree. I think the fallback with conversion success
> > > is a reasonable use case. Vladimir's case of notifying the
> > > user when a fallback value is being used is reasonable.
> > > It's difficult to leave that logic outside the conversion
> > > because only the conversion knows if and why the input is
> > > invalid.
> >
> > I agree with Matt.
>
> I don't see the added value then. If the user needs to check if
> the conversion succeeded, why not setting herself the fail-back
>
> int i;
> if (! try_convert_to(s,i))
> {
> i = fail_back;
> }
What are we comparing against? I've lost track. Shall we assume the default_value<T> customization point?
> > > string s = "4-2";
> > >
> > > // Matt likes to throw:
> > > int i = convert_cast(s);
> > >
> > > // Vladimir cares about success:
> > > int i = 17; if (!try_convert(s,i))
> > > { /* log that fallback value is being used */ }
> > >
> > > // except when he doesn't (but he never throws)
> > > int i = convert_cast(s, 17);
> > >
> > > // Vincente thinks success is optional:
> > > optional i = optional_convert(s);
> > >
> > > Note that in several of these, target typename is no longer
> > > needed, right?
> >
> > try_convert(s, i) doesn't tell me whether s is being
> converted to i's type or i to s's type.
>
> Is try_convert_to(s,i) better, or you see the same problem. Do
> you think it is better to use named parameters _from_ and _to?
It's try_convert_to<int>(s, i) which is better. "_to<int>" makes it clear.
> > convert_cast(s, 17) has the same problem and breaks with the
> > new-style cast pattern.
>
> would you prefer to use a _failback named parameter?
s/failback/fallback/
My concern was that it needs to be convert_cast<int>(s, 17).
> If try_convert_to doesn't updates the target variable if
> conversion fails we can even write
>
> int i=17;
> try_convert_to(s,i);
I think that's the right semantic.
_____
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