|
Boost : |
From: Unai Uribarri Rodríguez (unaiur_at_[hidden])
Date: 2003-01-22 16:34:14
Douglas Gregor wrote:
>
> 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?
I prefer to use any_cast<ValueType&>(x) to get a reference, since you
state clearly your intention. Also, both static_cast and dynamic_cast
uses that syntax.
I think that any_cast<ValueType>(x) shouldn't be equivalent to
any_cast<ValueType&>(x), therefore any_cast<ValueType>(x) must have
other semantics. It can return a copy or be illegal.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk