Boost logo

Boost Users :

From: Matthew Dempsky (jivera_at_[hidden])
Date: 2003-02-20 16:13:32


I'm curious if there's any way to get code like the following to work:

boost::any a = char(10);
int i = boost::any_cast<int>(a);

More specifically, what I mean is, I'd like to be able to place a value
of type T1 in 'a' and then retrieve it as type T2 which it can be
implicitly cast to. Is this possible without the part that extracts the
value from 'a' adding a lot of extra code to test all possibilities?

What I would like to do is fill a map<string, any> with function
pointers/objects/etc. and pass it to a function which casts the any
values to slot types to connect to different signals whose type is
determined at runtime. Basically something like this (but with MySig an
unknown type until runtime).

int func(char c, int i = 10);

boost::any a = &func;

typedef boost::signal<void (char)> MySig;
MySig sig;
sig.connect(boost::any_cast<MySig::slot_type>(a));
sig('j');

-jivera


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