【问题标题】:Compare boost::any contents比较 boost::any 内容
【发布时间】:2023-03-24 16:12:02
【问题描述】:

我正在使用一个容器来保存指向任何东西的指针列表:

struct Example {
    std::vector<boost::any> elements;
}

为了在这个容器中插入元素,我编写了几个辅助函数(struct Example 的成员):

void add_any(boost::any& a) {
    elements.push_back(a);
}

template<typename T>
void add_to_list(T& a) {
    boost::any bany = &a;
    add_any(bany);
}

现在,我只想在元素不存在于此容器中时插入元素。为此,我认为我只需要使用适当的比较器函数通过 elements 调用 search。但是,我不知道如何比较 boost::any 实例。

我的问题: 知道我的boost::any 实例总是包含指向某物的指针;是否可以比较两个 boost::any 值?


更新

感谢您的回答。我还设法以一种可能不安全的方式做到这一点:使用boost::unsafe_any_cast 获取void** 并比较底层指针。

目前,这工作正常。但是,我会感谢您的 cmets:也许这是一个大错误!

#include <boost/any.hpp>
#include <iostream>
#include <vector>
#include <string>
using namespace std;

bool any_compare(const boost::any& a1, const boost::any& a2) {
    cout << "compare " << *boost::unsafe_any_cast<void*>(&a1)
         << " with:  " << *boost::unsafe_any_cast<void*>(&a2);
    return (*boost::unsafe_any_cast<void*>(&a1)) ==
        (*boost::unsafe_any_cast<void*>(&a2));
}

struct A {};

class Example {
public:
    Example() : elements(0),
                m_1(3.14),
                m_2(42),
                m_3("hello"),
                m_4() {};
    virtual ~Example() {};

    void test_insert() {
        add_to_list(m_1);
        add_to_list(m_2);
        add_to_list(m_3);
        add_to_list(m_4);
        add_to_list(m_1); // should not insert
        add_to_list(m_2); // should not insert
        add_to_list(m_3); // should not insert 
        add_to_list(m_4); // should not insert
    };

    template <typename T>
    void add_to_list(T& a) { 
        boost::any bany = &a;
        add_any(bany);
    }

private:
    vector<boost::any> elements;
    double m_1;
    int    m_2;
    string m_3;
    A      m_4;


    void add_any(const boost::any& a) {
        cout << "Trying to insert " << (*boost::unsafe_any_cast<void*>(&a)) << endl;
        vector<boost::any>::const_iterator it;
        for (it =  elements.begin();
             it != elements.end();
             ++it) {
            if ( any_compare(a,*it) ) {
                cout << " : not inserting, already in list" << endl;
                return;
            }
            cout << endl;
        }
        cout << "Inserting " << (*boost::unsafe_any_cast<void*>(&a)) << endl;
        elements.push_back(a);
    };


};



int main(int argc, char *argv[]) {

    Example ex;
    ex.test_insert();
    unsigned char c;
    ex.add_to_list(c);
    ex.add_to_list(c); // should not insert

    return 0;
}

【问题讨论】:

  • 旁注:您的 add_anyadd_to_list 函数应该将 const-reference 作为参数。此外,如果您想要唯一性,您可以考虑使用set 而不是vector。当然,您仍然需要比较功能。
  • 好问题。这可能是不可能的。但是,如果您愿意实现 您自己的 any,有一种方法可以做到(如果基础类型都相同,则 run time fail,但是不支持比较语义)
  • 您确定要将参数的地址存储到add_to_list 助手吗?请注意,boost::any 将复制对象,但在这种情况下,对象是一个指针。
  • @David:是的,这正是我想要的。我不想复制对象(可能很大或不可复制)。
  • 有什么理由不使用void* 那么?

标签: c++ boost boost-any


【解决方案1】:

您不能直接提供它,但您实际上可以使用 any 作为基础类型...尽管对于指针来说它毫无意义(啊!)

struct any {
  std::type_info const& _info;
  void* _address;
};

还有一个模板化的构造函数:

template <typename T>
any::any(T* t):
   _info(typeid(*t)),
   _address(dynamic_cast<void*>(t))
{
}

这基本上是boost::any

现在我们需要用我们的比较机制来“增强”它。

为此,我们将“捕获”std::less 的实现。

typedef bool (*Comparer)(void*,void*);

template <typename T>
bool compare(void* lhs, void* rhs) const {
  return std::less<T>()(*reinterpret_cast<T*>(lhs), *reinterpret_cast<T*>(rhs));
}

template <typename T>
Comparer make_comparer(T*) { return compare<T>; }

并扩充any的构造函数。

struct any {
  std::type_info const& _info;
  void* _address;
  Comparer _comparer;
};

template <typename T>
any::any(T* t):
  _info(typeid(*t)),
  _address(dynamic_cast<void*>(t)),
  _comparer(make_comparer(t))
{
}

然后,我们提供了less(或operator&lt;)的专业化

bool operator<(any const& lhs, any const& rhs) {
  if (lhs._info.before(rhs._info)) { return true; }
  if (rhs._info.before(lhs._info)) { return false; }
  return (*lhs._comparer)(lhs._address, rhs._address);
}

注意:封装等...留给读者作为练习

【讨论】:

    【解决方案2】:

    我能想到的唯一简单方法是对您存储在 any 实例中的类型进行硬编码支持,这会破坏 any 的大部分实用性...

    bool equal(const boost::any& lhs, const boost::any& rhs)
    {
        if (lhs.type() != rhs.type())
            return false;
    
        if (lhs.type() == typeid(std::string))
            return any_cast<std::string>(lhs) == any_cast<std::string>(rhs);
    
        if (lhs.type() == typeid(int))
            return any_cast<int>(lhs) == any_cast<int>(rhs);
    
        // ...
    
        throw std::runtime_error("comparison of any unimplemented for type");
    }
    

    使用 C++11 的 type_index,您可以使用 std::mapstd::unordered_map 键入 std::type_index(some_boost_any_object.type()) - 类似于 Alexandre 在下面的评论中建议的内容。

    【讨论】:

    • a map&lt;std::type_info*, bool(const any&amp;, const any&amp;)&gt; 应该更具可扩展性。
    • @Alexandre:虽然不能可靠地工作......我记得你可以有多个 type_info 相同类型的对象(每个翻译单元,不保证被链接器删除),所以您需要将type_infos 与提供的operator== 进行比较...即使从type_info-&gt;name() 映射也是不可移植的,因为允许实现提供任何值,包括跨类型的空字符串或常量值。 type_info 太特别了,你一定会爱上它!
    • @Alexandre: std::type_info::before
    【解决方案3】:

    如果您可以更改容器中的类型,则有Boost.TypeErasure。它提供了自定义any 的简便方法。例如,我将此类 typedef 用于类似目的:

    #include <boost/type_erasure/any.hpp>
    #include <boost/type_erasure/operators.hpp>
    
    using Foo = boost::type_erasure::any<
        boost::mpl::vector<
            boost::type_erasure::copy_constructible<>,
            boost::type_erasure::equality_comparable<>,
            boost::type_erasure::typeid_<>,
            boost::type_erasure::relaxed
        >
    >;
    

    Foo 的行为与boost::any 完全相同,只是可以比较是否相等并使用boost::type_erasure::any_cast 而不是boost::any_cast

    【讨论】:

      【解决方案4】:

      无需创建新类。尝试使用 xany https://sourceforge.net/projects/extendableany/?source=directory xany 类允许向任何现有功能添加新方法。顺便说一句,文档中有一个示例完全符合您的要求(创建可比较的任意)。

      【讨论】:

      • 似乎无法在 clang Apple LLVM 版本 5.0 (clang-500.2.78) 上编译(基于 LLVM 3.3svn)目标:x86_64-apple-darwin13.0.0 还有什么许可证代码?
      【解决方案5】:

      也许这个算法会派上用场> http://signmotion.blogspot.com/2011/12/boostany.html

      按类型和内容比较两个任意值。尝试将字符串转换为数字以求等号。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-04
        • 2011-04-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        相关资源
        最近更新 更多