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

How to use struct of template class in own struct?

我在程序 A 中,我正在声明一个名为 Node 的结构,使用另一个在模板 class ab_int 由我正在使用的库 B 提供。

程序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:

中包含结构 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);
    }
    ...
}

lib 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;

更新二:

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

那是因为ab_int不是类型,它是模板:

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

并且您正在像类型一样使用它:

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

因此 "expected a class or namespace" 上的错误。你不能在语法上使用这样的模板。正确的语法是提供 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

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