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_; };

そしてここで私がメインでどのように使用しているか。

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;

最後のエラー:2つの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_3つの異なるインデックスへの参照を取得するヘルパーメソッドを作成する必要があります。これにより、物事がはるかに簡潔になります。例えば:

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 に答える