2010-12-17 39 views
1

为了避免msvc2010编译器错误,我使用一个用户自定义键extrator在composite_key这样的:的boost :: multi_index用户定义键提取和复合键

enum NodeType 
{ 
    TypeOne = 0, 
    TypeTwo 
}; 

struct TypeExtractor 
{ 
typedef NodeType result_type; 

const result_type& operator()(const boost::shared_ptr<Node>& p) const 
{ 
    return p->getNodeType(); 
}   
}; 

struct byValueAndType{}; 

typedef boost::multi_index_container< 
boost::shared_ptr<Node>, 
boost::multi_index::indexed_by< 
    boost::multi_index::random_access<>, 
    boost::multi_index::ordered_non_unique< 
    boost::multi_index::tag<byValueAndType>, 
    boost::multi_index::composite_key< 
    Node, 
    boost::multi_index::const_mem_fun<Node, const std::string&, &Node::getValue>, 
    TypeExtractor 
    > 
    > 
> 
> NodeList; 

typedef NodeList::nth_index<1>::type NodeListByValueAndType; 

这似乎很好编译在我老的实现,这是好的,因为我composite_key是“由”两个const_mem_fun

std::pair<Node::NodeListByValueAndType::const_iterator, Node::NodeListByValueAndType::const_iterator> range; 

range = _listNode.get<byValueAndType>().equal_range(boost::make_tuple("MyVal", Node::TypeOne)); 

:(和VC编译器不会再崩溃),但我有一些问题,当我尝试调用equal_range。现在composite_key的最后一个参数是一个自定义的键提取器,我不知道用'替换'Node :: TypeOne'。 (在我的equal_range调用)

编译器说,他期待的类型const boost::shared_ptr<Node>&,但我不希望创建一个共享指针,以良好的类型随机节点只为equal_range ...(和它不反正工作)

回答

1

使用下列组合键提取:

boost::multi_index::composite_key< 
    boost::shared_ptr<Node>, 
    boost::multi_index::const_mem_fun<Node, const std::string&, &Node::getValue>, 
    TypeExtractor 
> 
+0

谢谢你,仅此而已。 – yann 2010-12-18 19:23:38