4

我需要创建一个包含多索引容器的通用类作为存储。当我编译时,它给出了如下错误,我定义了第 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,smpp_config > smpp_conn_mgr_t; smpp_conn_mgr_t conn_mgr; }

4

1 回答 1

12

对嵌套的 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仅在模板中才需要这种特殊用途。

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),如此所述。


回应您的最后评论:此代码段为我编译:

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_索引的引用。这将使事情变得更加简洁。例如:

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);
}
于 2011-03-10T05:42:53.317 回答