1

为了避免 msvc2010 编译器错误,我在 Composite_key 中使用了用户定义的密钥提取器,如下所示:

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;

这似乎编译得很好(并且 vc 编译器不再崩溃),但是当我尝试调用 equal_range 时我遇到了一些问题:

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

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

在我的旧实现中没问题,因为我的composite_key 是由两个const_mem_fun“组成”的。现在,composite_key 的最后一个参数是自定义密钥提取器,我不知道用什么替换 'Node::TypeOne'。(在我的 equal_range 通话中)

编译器说他期待一个类型const boost::shared_ptr<Node>&,但我真的不想为 equal_range 创建一个指向好类型的随机节点的共享指针......(无论如何它都不起作用)

4

1 回答 1

2

使用以下复合键提取器:

boost::multi_index::composite_key<
    boost::shared_ptr<Node>,
    boost::multi_index::const_mem_fun<Node, const std::string&, &Node::getValue>,
    TypeExtractor
>
于 2010-12-17T07:00:51.967 回答