比较boost ::任何内容

| 我正在使用一个容器来保存指向任何内容的指针列表:
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
。但是,我不知道如何比较“ 5”个实例。 我的问题: 知道我的“ 5”实例总是包含指向某物的指针;是否可以比较两个“ 5”值? 更新 感谢您的回答。我还设法以一种可能不安全的方式做到了这一点:使用
boost::unsafe_any_cast
获得
void**
并比较基础指针。 目前,这很好。但是,我感谢您的评论:也许这是一个大错误!
#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;
}
    
已邀请:
我能想到的唯一简单的方法包括对您存储在
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::type_index(some_boost_any_object.type())
的a15ѭ或
std::unordered_map
,这与Alexandre在下面的评论中建议的类似。     
您不能直接提供它,但实际上可以使用
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))
{
}
这基本上是5英镑。 现在,我们需要使用比较机制对其进行“增强”。 为了做到这一点,我们将“捕获”实施“ 22”。
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>; }
并增加ѭ11的构造函数。
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<
)的专业化信息
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);
}
注意:封装等...作为练习留给读者     
如果您可以更改容器中的类型,则可以使用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
完全相同,不同之处在于can31 equality可以进行相等比较,并使用
boost::type_erasure::any_cast
代替
boost::any_cast
。     
无需创建新类。尝试使用xany https://sourceforge.net/projects/extendableany/?source=directory xany类允许向任何现有功能添加新方法。顺便说一句,文档中有一个示例可以完全满足您的要求(创建compare_any)。     
也许此算法派上用场> http://signmotion.blogspot.com/2011/12/boostany.html 比较类型和内容的两个任意值。尝试将字符串转换为数字以等于。     

要回复问题请先登录注册