boost :: multi_index用户定义的密钥提取器和组合密钥 [英] boost::multi_index user-defined key extractor and composite key

查看:74
本文介绍了boost :: multi_index用户定义的密钥提取器和组合密钥的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To avoid a msvc2010 compiler bug, I'm using a user-defined key extrator in a composite_key like that :

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时遇到一些问题:

This seems to compile well (and vc compiler does not crash anymore), but I have some problems when I try to call an 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通话中)

in my older implementation that was ok, as my composite_key was 'made of' two const_mem_fun. Now the last argument of the composite_key is a custom key extractor and I don't know what to replace 'Node::TypeOne' with. (in my equal_range call)

编译器说他期望的是const boost::shared_ptr<Node>&类型,但是我真的不希望仅针对equal_range来创建指向良好类型的随机Node的共享指针...(并且不起作用)无论如何)

the compiler says that he's expecting a type of const boost::shared_ptr<Node>&, but I don't realy want to create a shared pointer to a random Node of the good type just for the equal_range... (and it doesn't work anyway)

推荐答案

使用以下组合键提取器:

Use the following composite key extractor:

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

这篇关于boost :: multi_index用户定义的密钥提取器和组合密钥的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆