boost::有比较价值吗?
我有一个 boost::any 向量,想找到这个向量中“any”的索引。
像这样的事情:
vector<any> values;
any valueISearch = ...;
find<A*>(valueISearch);
为此,我尝试使用以下方法比较 2 个任何值:
template <class T> bool IsValueEqualTo(any aniInVector, any value)
{
if (aniInVector.empty() && value.empty())
return true;
if (aniInVector.empty() && !value.empty())
return false;
if (!aniInVector.empty() && value.empty())
return false;
try
{
T left = boost::any_cast<T>(aniInVector);
T right = boost::any_cast<T>(value);
return left == right;
}
catch(const boost::bad_any_cast &exception)
{
}
return false;
}
问题是,现在当我执行任播时,它只转换为特定类型,而不关心基本类型:
class A {};
class B: public A {};
B v1;
B v2;
IsValueEqualTo<A*>(&v1, &v2);
I have a vector of boost::any and would like to find the index of a 'any' in this vector.
Something like this :
vector<any> values;
any valueISearch = ...;
find<A*>(valueISearch);
For this I try to compare 2 any values with the following method :
template <class T> bool IsValueEqualTo(any aniInVector, any value)
{
if (aniInVector.empty() && value.empty())
return true;
if (aniInVector.empty() && !value.empty())
return false;
if (!aniInVector.empty() && value.empty())
return false;
try
{
T left = boost::any_cast<T>(aniInVector);
T right = boost::any_cast<T>(value);
return left == right;
}
catch(const boost::bad_any_cast &exception)
{
}
return false;
}
The problem is that now when I do a anycast it only cast to the specific type and don't care about base types :
class A {};
class B: public A {};
B v1;
B v2;
IsValueEqualTo<A*>(&v1, &v2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您的问题是
boost::any
库中存在一个基本限制,导致这些类型的转换失败。更一般地说,如果您尝试检索相同类型的对象,则只能使用any_cast
从boost::any
恢复对象。例如,这不起作用:这是 人们一直在抱怨的事情大约有一段时间,但我不知道解决方法。我认为您可能想做的一件事是首先考虑为什么要将 boost::any 与多态性混合在一起。您很有可能绝对必须这样做,但我没有找到在不最大限度地减少
boost::any
的使用的情况下解决此问题的好方法。I think your problem is that there is a fundamental limitation in the
boost::any
library that causes these sorts of casts to fail. More generally, you can only recover an object from aboost::any
usingany_cast
if you try to retrieve an object of identical type. For example, this doesn't work:This is something that people have been complaining about for a while and I don't know a workaround. I think that one thing you might want to do is consider why you're mixing
boost::any
with polymorphism in the first place. It's quite possible that you absolutely must do this, but I don't see a good way to fix this without minimizing your use ofboost::any
.尝试使用 xany https://sourceforge.net/projects/extendableany/?source=directory xany 类允许向任何现有功能添加新方法。顺便说一句,文档中有一个示例,它完全符合您的要求(创建可比较的_any)。
Try using xany https://sourceforge.net/projects/extendableany/?source=directory xany class allows to add new methods to any's existing functionality. By the way there is a example in documentation which does exactly what you want (creates comparable_any).
该算法按类型和内容比较两个 boost::any 值(尝试将字符串转换为等于数字 - lazy equals :) http://signmotion.blogspot.com/2011/12/boostany.html
This algorithm compares two boost::any values by type and content (attempt convert string to number for equals - lazy equals :) http://signmotion.blogspot.com/2011/12/boostany.html