c++ - 用于 boost 多索引容器的模板参数

标签 c++ templates boost boost-multi-index

我需要创建一个包含多索引容器的通用类作为存储。当我编译时,它会在我定义第 n 个索引 View 的地方出现如下错误。

错误:非模板“nth_index”用作模板


/**
 * connection manager
 */<p></p>

<p>template < typename T, typename C >
class conn_mgr: boost::noncopyable {
public:
    /**
     * connection ptr
     */
    typedef boost::shared_ptr conn_ptr_t;<br/>
    /**
     * 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;</p>

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

最佳答案

对嵌套的 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) ,如文件所示here .


回应你最后的评论:这个片段为我编译:

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

在哪里fooconn_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);
}

关于c++ - 用于 boost 多索引容器的模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5255515/

相关文章:

c++ - 什么时候是浮点运算 'invalid' ?

c++ - 禁用模板类中的函数

C++ createObject() 工厂

c++ - 有没有办法在 c++ 的 uint64_t 中获取日期的 utc 纳秒

c++ - 继承中的boost库

c++ - 如何在递归模板函数中首次调用时执行函数?

c++ - 通过引用传递时 std::vector 的转换器

C++从文本文件中读取,双行

c++ - 将文本文件单词插入 AVL

c++ - VTK:直到用户交互后 View 才会更新