|
Boost : |
Subject: Re: [boost] [proposal] safe_ref<T>
From: Fernando Pelliccioni (fpelliccioni_at_[hidden])
Date: 2012-12-13 19:37:20
On Thu, Dec 13, 2012 at 7:35 AM, Andrey Semashev
<andrey.semashev_at_[hidden]>wrote:
> On Thu, Dec 13, 2012 at 2:14 PM, Rob Stewart <robertstewart_at_[hidden]>
> wrote:
> > On Dec 13, 2012, at 2:18 AM, Nevin Liber <nevin_at_[hidden]> wrote:
> >
> >> On 13 December 2012 01:05, Andrey Semashev <andrey.semashev_at_[hidden]
> >wrote:
> >>
> >>> I don't see how this is better than optional<T&>, sorry. Your Safe
> wrapper could have been written around optional with the same success, and
> using lambdas or other function objects to work with references seem
> overcomplicated to me.
> >>
> >> +1
> >>
> >>> optional<int&> si(temp)
> >>> if (si)
> >>> std::cout << *si << std::endl;
> >>>
> >>
> >> Or even:
> >>
> >> int* pi(&temp);
> >> if (pi)
> >> std::cout << *pi << std::endl;
> >
> > While I mostly agree with you and Andrey, neither alternative addresses
> the OP's desire to force the null check by virtue of using the wrapper
> type. Using optional comes close, as there will be an exception, IIRC.
>
> An assert. Still, I would prefer optional or pointer in this case as
> the code looks clearer.
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>
Thank you all for your responses.
The idea is to avoid dereferencing a null reference, enforced by the
compiler (type safe).
optional<T&> not meet this purpose.
I'm thinking of a proposal involving certain changes in the core language.
But for now, I thought that this simple tool class library can help
(Obviously, I need to think of all possible uses).
Thanks and regards,
Fernando Pelliccioni.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk