Boost logo

Boost :

From: Unai Uribarri (unaiur_at_[hidden])
Date: 2003-01-21 14:54:17


Currently, any_cast doesn't allow to cast to references. Instead, you
have obscure castings from any* to T returning T*.

I think that the following code is more standard conforming and,
therefore, more understandable than the currently allowed counterparts:

       any x=int(5);
       ++any_cast<int&>(x);
        // instead of: ++*any_cast<int>(&x)

or,

       any x=list<int>();
       any_cast<list<int>&>(x).push_back(5);
        // instead of: any_cast<list<int> >(&x)->push_back(5);
       accumulate(any_cast<list<int> const&>(x).begin(),
         any_cast<list<int> const&>(x).end(), int(0));
        // instead of:
        // accumulate(any_cast<list<int> >(&x)->begin(),
        // any_cast<list<int> >(&x)->end(), int(0));

This patch doesn't affect either the former interface nor the
portability of Boost.any (since the type_traits/transform_traits_test
test is more portable that any/any_test, at least in the compiler status
   page)

Also, I propose to declare the former interface obsolete.

It's a patch against CVS on january, 20th.


--- any.hpp.orig 2003-01-20 21:45:31.000000000 +0100
+++ any.hpp 2003-01-20 21:47:30.000000000 +0100
@@ -18,6 +18,7 @@
 #include "boost/visitor/bad_visit.hpp"
 #include "boost/visitor/dynamic_visitable_base.hpp"
 #include "boost/visitor/try_dynamic_visit.hpp"
+#include "boost/type_traits.hpp"
 
 namespace boost
 {
@@ -207,12 +208,22 @@
     template<typename ValueType>
     ValueType any_cast(const any & operand)
     {
- const ValueType * result = any_cast<ValueType>(&operand);
+ typedef ::boost::remove_reference<ValueType>::type type;
+ const type * result = any_cast<type>(&operand);
         if(!result)
             throw bad_any_cast();
         return *result;
     }
 
+ template<typename ValueType>
+ ValueType any_cast(any & operand)
+ {
+ typedef ::boost::remove_reference<ValueType>::type type;
+ type * result = any_cast<type>(&operand);
+ if(!result)
+ throw bad_any_cast();
+ return *result;
+ }
 }
 
 // Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.


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