Boost logo

Boost Users :

From: Digvijoy Chatterjee (digvijoy.c_at_[hidden])
Date: 2007-02-27 22:49:04


  Hi,

 I have a function template like :

 template <class A> A what_type ( const boost::any &operand ){
         A a;
       a = boost::any_cast<a>(operand);
         return a;
 }
The idea is to provide the client code an easy way extract the type
hidden by the boost::any value,
 Now there is no way to make the compiler deduce the type to return
from an expression like below:

 std::string s = "hello";
 boost::any a = s;
 some_op( what_type(a) ); //calls some_op(string &);

Instead now the client has to write code using type()
 which returns std::typeinfo &

 if ( a.type().name() == "Ss" )
         some_op(what_type<string> a);
else if ( a.type().name() == "d" )
         some_op(what_type<double> a);

 Or use functions to test type and cast:

 if ( is_string (a) )
          some_op( what_type<string>(a));
 else ( is_int(a) )
         some_op ( what_type<int>(a));
 …..

 Is there any elegant way to handle this problem ??

 --Digz


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net