【发布时间】:2013-04-19 09:52:34
【问题描述】:
我想用 boost multi_index_container 和composite_key 实现这样的目标:
struct LogicalAnd {
bool operator()(const int& argVal1, const int& argVal2) const {
return int(argVal1 & argVal2) == argVal1;
}
};
template<class T, class Enable = void>
class FooContainer;
template <class T>
struct FooContainer<T,typename boost::enable_if<boost::is_base_of<IFoo, T> >::type> {
typedef multi_index_container<
boost::shared_ptr<T>,
indexed_by<
hashed_non_unique<
composite_key<
T,
const_mem_fun<T,int,&T::getKey1>,
const_mem_fun<T,int,&T::getKey2>
>,
composite_key_equal_to<
LogicalAnd,
LogicalAnd
>
>
>
> shared_ptr_type;
};
知道:
namespace CustomKey {
typedef enum {
VAL1 = 0x00000001,
VAL2 = 0x00000002,
ALL = 0xFFFFFFFF
} type;
}
目标是能够执行以下操作:
container.find(boost::make_tuple(CustomKey::VAL1, CustomKey::ALL));
这将允许我检索 LogicalAnd 返回 true 的所有元素。
问题是我无法让我的 LogicalAnd 比较器与我的 multi_index_container 一起工作。
我可以通过在composite_key_equal_to 之前添加composite_key_hash 来构建它:
composite_key_hash<
boost::hash<int>,
boost::hash<int>
>
但是 find 操作并没有按预期工作,所以它并没有太大变化......
我搜索了 boost 文档,并尝试了各种实现,但我被大量的信息淹没了......
感谢任何帮助!
【问题讨论】:
-
如果有帮助,这里有一个测试程序:coliru.stacked-crooked.com/…
-
至少有人知道吗? :)
-
至少还在寻找潜在客户?
-
您实际上并没有说明问题所在。你得到什么构建错误? 如何
find没有按预期工作? -
您好,我在coliru.stacked-crooked.com/… 上面链接了一个测试程序。如前所述,只要我指定
composite_key_hash,我就不会收到任何构建错误,但我希望 find 返回一个与我的LogicalAnd::operator匹配的项目。不幸的是,如链接代码所示,它不起作用,因为find不返回任何条目。
标签: c++ boost multi-index boost-multi-index