我正在使用一个容器来保存指向任何内容的指针列表:
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**
并比较底层指针.
目前,这工作正常。但是,我会感谢您的评论:也许这是一个大错误!
#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
作为底层类型......虽然对于指针来说它毫无意义(啊!)
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
.
现在我们需要用我们的比较机制“boost ”它。
为此,我们将“捕获”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<
)
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);
}
注意:封装等...留给读者作为练习
关于c++ - 比较 boost::any 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6029092/