簡體   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