Boost logo

Boost Users :

From: joel falcou (joel.falcou_at_[hidden])
Date: 2008-08-18 14:17:21


Let's say i have a hierarchy of object that all inherits from a abse class
called base_impl.
Among those classes, most of them are tempalte that
have large, complex type.

My idea was to use boost::any to
store any of this classes' instances, any_cast them back to base_impl* and
call one of the base_impl virtual method.

Basically I did this
:

  struct expression_impl
  {
    virtual ~expression_impl() {}
   
virtual void evaluate() const = 0;
  };

  struct
expression
  {
    template<class T>
expression( T const& x ) : mExpr(x) {}

   
void evaluate() {
boost::any_cast<expression_impl*>(mExpr)->evaluate(); }
    boost::any mExpr;
  };

Alas this
fails in a bad_any_cast exception. Considering that the basic use case of
expression is :

expression e =
/some_function_making_emporary_base_impl_instance();

I can't
rely on stroing a pointer in the boost::any member.
Any solution to
this problem or am I doing something wrong.



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