简体   繁体   中英

How to use struct of template class in own struct?

Im program A, I am declaring a struct called Node using another struct called node_type defined in template class ab_int provided by a lib BI am using.

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

This is the template containing struct node_type in lib B:

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

In lib B, there is also a function root() I want to use.

In program A, I want to instantiate a new instance of struct Node :

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

I am getting the following compile error:

.../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 ) {}

Update 1:

After changing ab_int::node_type to ab_int<>::node_type , a different error popped up:

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

Update 2:

The second compile error was related with some inconsistencies regarding instantiation of object wt. Because isn't related to the original question, I don't discuss it any further.

That's because ab_int is not a type, it's a template :

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

And you are using it like a type:

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

Hence the error on "expected a class or namespace". You cannot syntactically use a template like that. The correct syntax would be to provide a specific instantiation of the ab_int template:

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

Alternatively, you could make Node itself a template to forward those types to ab_int (depending on how well this fits your use-case):

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

In that cause we'd need to provide the additional typename keyword, since now node_type becomes a dependent type.

If you wish to avoid manually specify template arguments I'd suggest reading up on: http://en.cppreference.com/w/cpp/language/template_argument_deduction

Using Template argument deduction can make templates more flexible and easier to maintain. Templates are about writing generic code, so manually providing a template type parameter limits that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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