Boost logo

Boost :

From: Unai Uribarri Rodríguez (unaiur_at_[hidden])
Date: 2003-01-14 16:39:27


I've modified any_cast to work properly with references.

In the boos 1.29 implementation, a code like this:

{
   any x=5;
   ++any_cast<int&>(x);
}

doesn't compile because any_cast tries to instantiate a pointer to int
&. With the attached patch (which uses ::boost::remove_reference in
boost/type_traits.hpp to translate int& to int) this code compile and
works as expected (at least as I expect, setting x to 6).

I believe that it doesn't break the interface for any_cast.

Is there any chance to get the patch accepted?

Good bye!


--- any.hpp.orig 2003-01-14 19:54:44.000000000 +0100
+++ any.hpp 2003-01-14 20:06:02.000000000 +0100
@@ -12,6 +12,7 @@
 #include <typeinfo>
 
 #include "boost/config.hpp"
+#include <boost/type_traits.hpp>
 
 namespace boost
 {
@@ -167,12 +168,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