简体   繁体   中英

How to initialize std::array member in constructor initialization list when the size of the array is a template parameter

In the following example, I need to initialize the std::array in the A::A(H h) constructor initializer list (because class H doesn't have a default constructor), but I can't do it with an initializer list since the array size is a template parameter.

Is there a way around this?

#include <array>
using namespace std;

struct Hash {
    const vector<int> &data;

    Hash(const vector<int> &data)
        : data(data) {
    }

    uint64_t operator()(int id) const {
        return data[id];
    }
};

template <class H, size_t N>
class A {
public:
    A(H h) {
    }

    std::array<H, N> hashes;
};
    

int main () {
    vector<int> data{1, 2, 3, 4};

    A<Hash, 4> a{Hash(data)};
}

std::index_sequence was provided in order to simplify the metaprogramming task of creating and expanding a pack whose size is not fixed. Use std::make_index_sequence and delegate the construction to some private constructor that deduces the pack:

A(H h) : A(h, std::make_index_sequence<N>{}) {}

template <std::size_t... i>
A(H h, std::index_sequence<i...>) : hashes{((void)i, h)...} {}

Here, ((void)i, h) is a comma expression that evaluates to h , but since we mentioned the pack i inside it, it's eligible to be expanded using ... . The result is N copies of h (one for each element of i ). This expansion occurs inside a braced-init-list, so the result is a braced-init-list contaning N copies of h , which will then initialize the hashes member.

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