比较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;
}
没有找到相关结果
已邀请:
5 个回复
渴翅吮斡撤
实例中的类型的硬编码支持,这削弱了
的许多实用性...
使用C ++ 11的
,您可以使用键入
的a15ѭ或
,这与Alexandre在下面的评论中建议的类似。
财沟项胶
作为基础类型...尽管对于指针来说,它毫无意义(啊!)
还有一个模板构造函数:
这基本上是5英镑。 现在,我们需要使用比较机制对其进行“增强”。 为了做到这一点,我们将“捕获”实施“ 22”。
并增加ѭ11的构造函数。
然后,我们提供了
(或
)的专业化信息
注意:封装等...作为练习留给读者
磨标烫徽啪
的简便方法。例如,我出于类似目的使用此类typedef:
的行为与
完全相同,不同之处在于can31 equality可以进行相等比较,并使用
代替
。
壬驴拿歪竞
眠缝