繁体   English   中英

如何在自己的结构中使用模板类的结构?

[英]How to use struct of template class in own struct?

在程序A中,我使用另一个由lib BI使用的模板类ab_int定义的名为node_type的结构声明了一个称为Node的结构。

程序A:

struct Node {
 int rank;
 ab_int::node_type nt;

 Node() : rank(), nt() {}
 Node( int rank_new, ab_int::node_type nt_new ) : rank( rank_new ), nt( nt_new ) {}
};

这是库B中包含struct node_type的模板:

template<class t_bitvector = bit_vector, ...>
class ab_int
{
   ...
   public: 
    struct node_type {
     ...
     // Assignment operator
     node_type& operator=(const node_type&) = default;

     // Move assignment operator
     node_type& operator=(node_type&&) = default;
     ...
    }

    ...
    node_type root() const {
        return node_type(0, m_size, 0, 0);
    }
    ...
}

在库B中,还有一个我想使用的函数root()

在程序A中,我想实例化struct Node的新实例:

// wt instantiated (works!)
Node node;
node.rank = 1;
node.nt = wt.root();

我收到以下编译错误:

.../main.cpp:23:5: error: expected a class or namespace
ab_int::node_type nt;
^
.../main.cpp:26:44: error: expected a class or namespace
Node( int rank_new, ab_int::node_type nt_new ) : rank( rank_new ), nt( nt_new ) {}

更新1:

ab_int::node_type更改为ab_int<>::node_type ,弹出另一个错误:

.../main.cpp:63:13: error: no viable overloaded '='
node.nt = wt.root();
~~~~~~~ ^ ~~~~~~~~~
.../ab_int.hpp:752:24: note: candidate function not viable: no known conversion from 'z::ab_int<z::rrr_vector<63, z::int_vector<'\x00'>, 32>, z::rank_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x00', 63, z::int_vector<'\x00'>, 32> >::node_type' to 'const z::ab_int<z::int_vector<'\x01'>, z::rank_support_v<'\x01', '\x01'>, z::select_support_mcl<'\x01', '\x01'>, z::select_support_mcl<'\x00', '\x01'> >::node_type' for 1st argument
        node_type& operator=(const node_type&) = default;
                   ^
.../ab_int.hpp:755:24: note: candidate function not viable: no known conversion from 'z::ab_int<z::rrr_vector<63, z::int_vector<'\x00'>, 32>, z::rank_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x01', 63, z::int_vector<'\x00'>, 32>, z::select_support_rrr<'\x00', 63, z::int_vector<'\x00'>, 32> >::node_type' to 'z::ab_int<z::int_vector<'\x01'>, z::rank_support_v<'\x01', '\x01'>, z::select_support_mcl<'\x01', '\x01'>, z::select_support_mcl<'\x00', '\x01'> >::node_type' for 1st argument
        node_type& operator=(node_type&&) = default;

更新2:

第二个编译错误与关于对象wt的实例化的一些不一致有关。 由于与原始问题无关,因此不再赘述。

那是因为ab_int不是类型,而是模板

template<class t_bitvector   = bit_vector, ...>
class ab_int { .. };

而且您像一种类型一样使用它:

struct Node {
   int rank;
   ab_int::node_type nt;
// ^^^^^^^^

因此,“预期类或名称空间”错误。 您不能在语法上使用这样的模板。 正确的语法是提供ab_int模板的特定实例化:

ab_int<bit_vector_type, ...>::node_type nt;
ab_int<>::node_type nt; // use defaults

或者,您可以将Node本身作为模板,以将这些类型转发到ab_int (取决于这适合您的用例):

template <typename t_bitvector = bit_vector, ...>
struct Node {
    typename ab_int<bitvector, ...>::node_type nt;
//  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
};

因此,我们需要提供附加的typename关键字,因为现在node_type成为从属类型。

如果您希望避免手动指定模板参数,建议您阅读以下内容: http : //en.cppreference.com/w/cpp/language/template_argument_deduction

使用模板参数推导可以使模板更灵活,更易于维护。 模板是关于编写通用代码的,因此手动提供模板类型参数将对此进行限制。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM