模板参数,以提高多指数容器 [英] template parameter to boost multi index container
问题描述
我需要创建一个包含多指标的容器作为存储一个泛型类。当我编译,它提供了如下错误,我已经定义了第n个索引视图。
I need to create a generic class containing multiindex container as a storage. when I compile, it gives error as below where I have defined nth index view.
错误:作为模板使用非模板nth_index
error: non-template ‘nth_index’ used as template
/**
* connection manager
*/
&模板LT; typename的T,typename的C>
类conn_mgr:升压:: noncopyable进行{
上市:
/ **
*连接PTR
* /
TYPEDEF提高:: shared_ptr的conn_ptr_t;结果
/ **
*连接表类型
*这是一个多指标容器
* /
TYPEDEF的boost :: multi_index :: multi_index_container的<
conn_ptr_t,
提高:: multi_index ::的indexed_by<
//测序< >
提高:: multi_index :: hashed_unique<
BOOST_MULTI_INDEX_CONST_MEM_FUN(T,标准::字符串,T :: ID)>,
提高:: multi_index :: hashed_non_unique<
BOOST_MULTI_INDEX_CONST_MEM_FUN(T,标准::字符串,
T ::型)>,
提高:: multi_index :: hashed_non_unique<
提高:: multi_index :: composite_key< conn_ptr_t,
BOOST_MULTI_INDEX_CONST_MEM_FUN(T,
标准::字符串,T :: ID)
BOOST_MULTI_INDEX_CONST_MEM_FUN(T,
标准::字符串,T ::型)>>>>
conn_table_t;
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_;
};
private: conn_table_t conn_table_; };
和这里我如何使用主。
INT主(INT ARGC,字符** argv的)
{
typedef的conn_mgr&LT; smpp_conn,smpp_config> smpp_conn_mgr_t;
smpp_conn_mgr_t conn_mgr;
}
int main( int argc, char** argv ) { typedef conn_mgr < smpp_conn, smpp_config > smpp_conn_mgr_t; smpp_conn_mgr_t conn_mgr; }
推荐答案
使用这个语法,而不是你的嵌套的typedef:
Use this syntax instead for your nested typedefs:
typedef typename conn_table_t::template nth_index<0>::type conn_table_by_id_type;
的类型名称
关键字用在这里作为资格让编译器知道 conn_table_t ::模板nth_index℃的&GT; ::类型
是一个类型。这种特殊的类型名称
使用只需要在模板。
The typename
keyword is used here as a qualifier to let the compiler know that conn_table_t::template nth_index<0>::type
is a type. This special use of typename
is only necessary within templates.
的模板
关键字用在这里作为一个限定从其他名称distingush成员模板。
The template
keyword is used here as a qualifier to distingush member templates from other names.
此外,该行是无效的:
typedef boost::shared_ptr conn_ptr_t;
您不能的typedef模板。你只能typedef的类型。也许你的意思写的:
You can't typedef templates. You can only typedef types. Perhaps you meant to write:
typedef typename boost::shared_ptr<T> conn_ptr_t;
最后一个错误:你试图给出两个类型定义相同的名称: conn_table_by_id_type
您应该使用 BOOST_MULTI_INDEX_CONST_MEM_FUN(T,标准::字符串,ID)
而不是 BOOST_MULTI_INDEX_CONST_MEM_FUN(T,标准::字符串,T :: ID)
,如记录<一个href=\"http://www.boost.org/doc/libs/release/libs/multi_index/doc/reference/key_extraction.html#boost_multi_index_const_mem_fun\">here.
You should use BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, id)
instead of BOOST_MULTI_INDEX_CONST_MEM_FUN(T, std::string, T::id)
, as documented here.
在回答您最后的评论:这段代码编译对我来说:
In response to your last comment: This snippet compiles for me:
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);
}
其中,富
是 conn_mgr
模板中的成员函数。我猜,上面是你试图做的。
Where foo
is a member function inside the conn_mgr
template. I'm guessing that the above is what you were trying to do.
您应该写helper方法得到你的三个不同的 conn_table _
索引引用。这会让事情变得更加简洁。例如:
You should write helper methods that get references to your three different conn_table_
indices. This will make things much more concise. For example:
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);
}
这篇关于模板参数,以提高多指数容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!