【问题标题】:template parameter to boost multi index container提升多索引容器的模板参数
【发布时间】:2011-07-12 10:45:34
【问题描述】:

我需要创建一个包含多索引容器作为存储的通用类。当我编译时,它给出了如下错误,我定义了第 n 个索引视图。

错误:非模板“nth_index”用作模板


/**
 * connection manager
 */

template < typename T, typename C > class conn_mgr: boost::noncopyable { public: /** * connection ptr */ typedef boost::shared_ptr conn_ptr_t;
/** * connection table type * It's a multi index container */ typedef boost::multi_index::multi_index_container < conn_ptr_t, boost::multi_index::indexed_by < //sequenced < >, boost::multi_index::hashed_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::id) >, boost::multi_index::hashed_non_unique < BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::type)>, boost::multi_index::hashed_non_unique < boost::multi_index::composite_key < conn_ptr_t, BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::id), BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::type ) > > > > conn_table_t;

//typedef for ConnectionIdView
typedef conn_table_t::nth_index<0>::type conn_table_by_id_type;

typedef conn_table_t::nth_index<1>::type conn_table_by_type;

typedef conn_table_t::nth_index<2>::type conn_table_by_id_type;

私人: conn_table_t conn_table_; };

这里是我在 main 中的使用方式。

int main(int argc, char** argv) { typedef conn_mgr smpp_conn_mgr_t; smpp_conn_mgr_t conn_mgr; }

【问题讨论】:

  • 您不能 typedef 模板。此行无效:typedef boost::shared_ptr conn_ptr_t;

标签: c++ templates boost boost-multi-index


【解决方案1】:

对嵌套的 typedef 使用此语法:

typedef typename conn_table_t::template nth_index<0>::type conn_table_by_id_type;

typename 关键字在这里用作限定符,让编译器知道conn_table_t::template nth_index&lt;0&gt;::type 是一个类型。 typename 的这种特殊用法仅在模板中是必需的。

template 关键字在此处用作区分成员模板与其他名称的限定符。


此外,此行无效:

typedef boost::shared_ptr conn_ptr_t;

您不能 typedef 模板。您只能 typedef 类型。也许你的意思是写:

typedef typename boost::shared_ptr<T> conn_ptr_t;

最后一个错误:您试图给两个 typedef 赋予相同的名称:conn_table_by_id_type


您应该使用BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, id) 而不是BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::id),如here 所述。


回应您的最后评论:这个 sn-p 为我编译:

void foo(std::string id)
{
    conn_table_by_id_type& id_type_view = conn_table_.template get<0>();
    typename conn_table_by_id_type::const_iterator it = id_type_view.find(id);
}

其中fooconn_mgr 模板内的成员函数。我猜上面是你想要做的。

您应该编写获取对三个不同conn_table_ 索引的引用的辅助方法。这将使事情变得更加简洁。例如:

conn_table_by_id_type & by_id_type() {return conn_table_.template get<0>();}

void foo2(std::string id)
{
    typename conn_table_by_id_type::const_iterator it = by_id_type().find(id);
}

【讨论】:

  • 埃米尔,非常感谢。我还学到了使用 typename 的课程。例如。当我尝试 std::vector::iterator 时,它给出了错误,但是一旦我转换为 typename std::vector::iterator,它就起作用了。
  • 嗨 Emile,现在当我使用上面显示的 conm_mgr 类时,我收到错误“在 smpp_conn 类中没有名为“T”的类型。我将“smpp_conn”类对象作为第一个模板参数“T " 在 conn_mgr 类中。
  • @Emile:那行得通。需要更多帮助。我如何找到和迭代器?例如 typedef typename conn_table_t::template nth_index::type conn_table_by_id_type; conn_table_by_id_type *id_type_view;类型名 conn_table_by_id_type::const_iterator it = id_type_view->find(id);对其标量转换给出错误。
  • @rjoshi:您不能在未初始化的指针 *id_type_view 上调用方法。请参阅修改后的答案。
  • @Emile:谢谢。我从索引 2 获取迭代器并分配给索引 1 的 id_view。但我喜欢你创建包装函数的想法。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 2016-11-07
相关资源
最近更新 更多