boost::有比较价值吗?

发布于 2024-10-11 18:37:04 字数 886 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧城烟雨 2024-10-18 18:37:04

我认为您的问题是 boost::any 库中存在一个基本限制,导致这些类型的转换失败。更一般地说,如果您尝试检索相同类型的对象,则只能使用 any_castboost::any 恢复对象。例如,这不起作用:

class A { ... };
class B: public A { ... };

boost::any a = new B*;
A* ptr = boost::any_cast<A*>(a); // Cast fails

这是 人们一直在抱怨的事情大约有一段时间,但我不知道解决方法。我认为您可能想做的一件事是首先考虑为什么要将 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 a boost::any using any_cast if you try to retrieve an object of identical type. For example, this doesn't work:

class A { ... };
class B: public A { ... };

boost::any a = new B*;
A* ptr = boost::any_cast<A*>(a); // Cast fails

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 of boost::any.

蒗幽 2024-10-18 18:37:04

尝试使用 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).

你丑哭了我 2024-10-18 18:37:04

该算法按类型和内容比较两个 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文