|
Boost : |
Subject: Re: [boost] [convert] no-throw and fallback feature
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-05-11 07:46:44
Gordon Woodhull wrote:
> On May 10, 2011, at 3:55 PM, Vicente BOTET wrote:
>
> I think opt_tie should behave as if it's optional<T>&. I am
> not sure why one would want to chain assignments in this case,
> but it should be possible.
If you can't explain why it is needed, I consider that a reason to reject it.
Since tie() doesn't work that way, it would be surprising. However, it would permit Vicente's use case:
int i(fallback);
if (!(opt_tie(i) = convert_to<optional<int>>(s))
> Unfortunately because optional<T&> wants to rebind its
> reference it's not possible to do as boost::tie does.
That is, indeed, unfortunate for this purpose.
> So, it's probably a wrapper.
Agreed.
> This works; I'm not sure if it's optimal:
>
> #include <boost/optional.hpp>
> #include <iostream>
>
> using boost::optional;
> using std::cout;
> using std::endl;
>
> template<typename T>
> class opt_tie_type {
> T &x_;
> public:
> opt_tie_type(T &x) : x_(x) {}
> const optional<T> operator=(optional<T> const& y) {
The return type should be optional<T> const & if not void.
> if(y)
> x_ = y.get();
> return y;
> }
> };
>
> template<typename T>
> opt_tie_type<T> opt_tie(T &x) {
> return opt_tie_type<T>(x);
> }
I don't think anything more optimal is possible after addressing the return type of the assignment operator. However, a robust implementation will need a PTS for reference types to address the reference-to-reference problem (or use remove_reference).
_____
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