存储升压功能
我必须存储不同 boost::function 对象的列表。为了提供这个,我使用 boost::any 。我有一些函数采用不同的函数签名,将它们打包到 any 中,然后插入到给定类型的特殊映射中。这是代码:
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 */ }
现在我有地图增强功能,将其打包到具有枚举中给定类型的 any 中,以便将来识别它。现在我必须调用给定的函数。任何类型的转换都不起作用:
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;
}
}
这不会转换,并且在运行过程中出现异常:
what(): boost::bad_any_cast: failed conversion using boost::any_cast
是否可以转换回 boost::function 对象?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
@TC 提供了运行时错误的解决方案。但我相信你应该使用 Boost.Variant 而不是 < a href="http://boost.org/doc/libs/1_44_0/doc/html/any.html" rel="nofollow noreferrer">Boost.Any 因为只有固定的类型选择可以存储。使用 Boost.Variant,您也可以消除该枚举,因为它已经提供了标准的访问者模式接口。 (结果):
@TC provided the solution for the runtime error. But I believe you should use Boost.Variant instead of Boost.Any as there are only a fixed selection of types it can store. With Boost.Variant you could eliminate that enum too, as it already provided a standard visitor pattern interface. (result):
从表面上看,row.first 是回调的名称,是一个字符串。您可能应该使用 row.second.first:
此外,您应该在 switch 中使用枚举常量 (
case CLASS::e_int:
),而不是幻数。From the looks of it,
row.first
is the name of the callback, astring
. You should probably userow.second.first
:Furthermore, you should use your enum constants in the switch (
case CLASS::e_int:
), instead of magic numbers.