模板参数来提升多索引容器 [英] template parameter to boost multi index container
问题描述
我需要创建一个包含multiindex容器作为存储的通用类。当我编译时,它给出错误如下我已经定义了第n个索引视图。
错误:非模板'nth_index'用作模板
/ **
*连接管理员
* /
template< typename T,typename C>
class conn_mgr:boost :: noncopyable {
public:
/ **
* connection ptr
* /
typedef boost :: shared_ptr conn_ptr_t;
/ **
*连接表类型
*它是一个多索引容器
* /
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;
// ConnectionIdView的typedef
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;
private:
conn_table_t conn_table_;
};
这里我如何使用在主。
int main(int argc,char ** argv)
{
typedef conn_mgr& smpp_conn,smpp_config> smpp_conn_mgr_t;
smpp_conn_mgr_t conn_mgr;
}
解决方案使用此语法代替嵌套typedef:
typedef typename conn_table_t :: template nth_index< 0> :: type conn_table_by_id_type;
typename
关键字在此处用作限定符,让编译器知道conn_table_t :: template nth_index< 0> :: type
是一个类型。typename
的特殊用途仅在模板中有用。
模板$
此外,此行是无效:
typedef boost :: shared_ptr conn_ptr_t;
不能使用typedef模板。你只能使用typedef类型。也许你打算写:
typedef typename boost :: shared_ptr< T& conn_ptr_t;
最后一个错误:两个typedef具有相同的名称:
conn_table_by_id_type
code> BOOST_MULTI_INDEX_CONST_MEM_FUN(T,std :: string,T :: id)而不是
BOOST_MULTI_INDEX_CONST_MEM_FUN c>,如此处所记录。
为了回复您最后的评论:此代码段为我编译:
<$ pre>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);
}
其中 foo
在 conn_mgr
模板中的成员函数。
您应该编写帮助方法来获取对您的三个不同的引用 conn_table_
indices。这将使事情更简洁。例如:
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);
}
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.
error: non-template ‘nth_index’ used as template
/**
* 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;
private: conn_table_t conn_table_; };
and here how I am using in main.
int main( int argc, char** argv ) { typedef conn_mgr < smpp_conn, smpp_config > smpp_conn_mgr_t; smpp_conn_mgr_t conn_mgr; }
Use this syntax instead for your nested typedefs:
typedef typename conn_table_t::template nth_index<0>::type conn_table_by_id_type;
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.
The template
keyword is used here as a qualifier to distingush member templates from other names.
Furthermore, this line is invalid:
typedef boost::shared_ptr conn_ptr_t;
You can't typedef templates. You can only typedef types. Perhaps you meant to write:
typedef typename boost::shared_ptr<T> conn_ptr_t;
One last error: You're attempting to give two typedefs the same name: conn_table_by_id_type
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);
}
Where foo
is a member function inside the conn_mgr
template. I'm guessing that the above is what you were trying to do.
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屋!