Boost logo

Boost :

From: Douglas Gregor (gregod_at_[hidden])
Date: 2003-01-22 09:06:26


On Wednesday 22 January 2003 08:35 am, Peter Dimov wrote:
> From: "Douglas Gregor" <gregod_at_[hidden]>
>
> > On Tuesday 21 January 2003 04:24 pm, David Abrahams wrote:
> > > Unai Uribarri <unaiur_at_[hidden]> writes:
> > > > Currently, any_cast doesn't allow to cast to
> > > > references. Instead, you have obscure castings from
> > > > any* to T returning T*.
> > >
> > > That sounds like a design mistake on the face of it.
> >
> > This comment has come up before, and everyone seems to agree that we'd
>
> prefer
>
> > the semantics that Unai proposes. I think the only reason that we haven't
> > changed the semantics is that we don't want to change Kevlin's design
>
> without
>
> > hearing the reasons for the existing design.
>
> The pointer cast returns NULL on failure, the value cast throws
> bad_any_cast.

That wouldn't change. The only difference is that the existing semantics
return a copy of the ValueType when the type is correct, and throws
bad_any_cast on failure. The signature is:

template<typename ValueType>
  ValueType any_cast(const any & operand);

In many cases, it would be more useful to get an actual reference to the held
value, so the folllowing signature(s) would be preferred:

template<typename ValueType> ValueType& any_cast(any & operand);
template<typename ValueType> const ValueType& any_cast(const any & operand);

If "operand" does not contain a value of type ValueType, any_cast throws
bad_any_cast. Otherwise, it returns a reference to the held value. Why
wouldn't we want this behavior?

        Doug


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk