Hello, I have to store a list of different boost::function objects. To provide this I'm using boost::any. I have a few functions which takes different functions signatures, pack them into any and then insert into special map with given type. Here is the code:
enum TypeEnumerator
{
e_int,
e_float,
e_double
};
typedef map< string, pair<any, TypeEnumerator> > CallbackType;
CallbackType mCallbacks;
void Foo(const string &name, function<float ()> f)
{
mCallbacks[name] = make_pair(any(f), CLASS::e_float);
}
void Foo(const string &name, function<int ()> f) { /* the same, but with e_int */ }
void Foo(const string &name, function<double ()> f) { /* the same, but with e_double */ }
Now I have in map boost function, packed into any with given type from enum, to recognize it in future. Now I have to call given functions. The casting from any won't work:
BOOST_FOREACH(CallbackType::value_type &row, mCallbacks)
{
// pair<any, TypeEnumerator>
switch (row.second.second) // Swith the TypeEnumerator
{
case 0: // int
any_cast< function<int ()> >(row.first)();
break;
case 1: // float
any_cast< function<float ()> >(row.first)();
break;
case 2: // double
any_cast< function<double ()> >(row.first)();
break;
}
}
This won't cast and during running I get the exception:
what(): boost::bad_any_cast: failed conversion using boost::any_cast
Is it possible to convert back the boost::function object?