|
Boost : |
Subject: Re: [boost] [review] string convert
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-04 15:33:20
Matthew Chambers wrote:
> On 5/4/2011 2:01 PM, Stewart, Robert wrote:
>
> >>> The target type should be required in all cases, I suspect,
> >>> to make things clearer:
> >>>
> >>> try_convert_to<int>(s, i) clearly indicates that int is the
> >>> target type.
> >>
> >> Yes, but thanks to type inference users are free to write
> >> try_convert_to(s, i) instead and that's even worse than
> >> try_convert(s, i). :)
> >
> > You can make the second argument non-deducible thus requiring
> > the target type.
>
> I didn't know that was possible. How? If the target is non-
> deducible then I agree the _to suffix for optional_convert
> and try_convert is good.
template <class T>
struct identity
{
typedef T type;
};
template <class T, class S>
T
try_convert_to(S, identity<T>::type);
identity<T>::type is T, but the compiler cannot deduce T from identity<T>::type. Therefore, T must be given specifically.
> Back to something you said earlier:
> >>> If others agree, I could also go with Vincente's version of
> >>> try_convert. It doesn't bother me that passing in the
> >>> variable by reference makes it a two-liner, since the
> >>> expression itself can go inside the if statement and it
> >>> implicitly supports non-default-constructable types.
> >
> > It is certainly non-surprising.
> >
> > Using Vicente's default_value customization point still makes
> > convert_cast<optional<T>> a viable candidate:
> >
> > auto const c(convert_cast<optional<int>>(s));
> > if (c)
> > {
> > // use c.get()
> > }
>
> What about UDT specializations for casting to and from
> optional<T> (i.e. string<->optional<int> where 'n/a'
> could be the unset string)?
I don't understand your question, I'm afraid.
_____
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