Template parameter for enlarging a container with multiple indices

I need to create a generic class containing a multiindex container as storage. when I compile it gives an error as below, where I defined the nth index.

error: non-template 'nth_index used as a 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 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, as I use mainly.

int main (int argc, char ** argv) {typedef conn_mgr <smpp_conn, smpp_config> smpp_conn_mgr_t; smpp_conn_mgr_t conn_mgr; }

+4
source share
1 answer

Use this syntax instead of 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, so that the compiler knows that conn_table_t::template nth_index<0>::type is a type. This special use of typename is only required in templates.

The template keyword is used here as a qualifier for distingush member templates from other names.


In addition, this line is invalid:

 typedef boost::shared_ptr conn_ptr_t; 

You cannot create typedef templates. You can use only typedef types. Perhaps you wanted to write:

 typedef typename boost::shared_ptr<T> conn_ptr_t; 

One last mistake: you are trying 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 described 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 guess this is what you tried to do.

You must write helper methods that get references to your three different conn_table_ . This will make things much more concise. For instance:

 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); } 
+11
source

Source: https://habr.com/ru/post/1343114/


All Articles