[any] bug? any_cast<>( any& ) returns by value

Hi, I was wondering why any_cast doesn't return a reference (as boost::get does for boost::variant). To me, it seems to make little sense to supply a any_cast overload with argument any& in addition to const any &, for return-by-value. It also needlessly invokes T's copy ctor, which esp bad if that one throws. Both observations considered, I think the implementation should rather be template <typename T> T & any_cast( any & ); template <typename T> const T & any_cast( const any & ); template <typename T> T * any_cast( any * ); template <typename T> const T * any_cast( const any * ); Don't you agree? Thanks, Marc -- Marc Mutz -- marc@klaralvdalens-datakonsult.se, mutz@kde.org phone: +49 521 521 45 45; mobile: +47 45 27 38 95 Klarälvdalens Datakonsult AB, Platform-independent software solutions

Marc Mutz wrote: <snip>
I was wondering why any_cast doesn't return a reference (as boost::get does for boost::variant). To me, it seems to make little sense to supply a any_cast overload with argument any& in addition to const any &, for return-by-value. It also needlessly invokes T's copy ctor, which esp bad if that one throws.
<snip> According to the documentation of boost::any_cast<T>, "if T is a reference to (possibly const qualified) ValueType, it [boost::any_cast] returns a reference to the held value." -delfin

On Monday 13 February 2006 20:08, Delfin Rojas wrote:
According to the documentation of boost::any_cast<T>, "if T is a reference to (possibly const qualified) ValueType, it [boost::any_cast] returns a reference to the held value."
Right. But this is still inconsistent both with variant's get<> and e.g.
static_cast<>. The static_cast<> behaviour can't be done in current C++
(since you can't partially specialise function templates), so maybe variant's
get<> should be changed?
Hmm, maybe I lied when I said static_cast<> syntax is not possible... Lemme
try. This should give static_cast<> semantics for any_cast (ie. <T> cast by
value,
participants (2)
-
Delfin Rojas
-
Marc Mutz