简体   繁体   English

当数组的大小是模板参数时,如何在构造函数初始化列表中初始化 std::array 成员

[英]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.在以下示例中,我需要在 A::A(H h) 构造函数初始化程序列表中初始化 std::array (因为 class H 没有默认构造函数),但我不能使用初始化程序来完成列表,因为数组大小是模板参数。

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.提供std::index_sequence是为了简化创建和扩展大小不固定的包的元编程任务。 Use std::make_index_sequence and delegate the construction to some private constructor that deduces the pack:使用std::make_index_sequence并将构造委托给一些推断包的私有构造函数:

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 ... .这里, ((void)i, h)是一个逗号表达式,计算结果为h ,但由于我们在其中提到了包i ,因此可以使用...对其进行扩展。 The result is N copies of h (one for each element of i ).结果是hN个副本( 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.这种扩展发生在一个花括号初始化列表中,因此结果是一个包含Nh副本的花括号初始化列表,然后它将初始化hashes成员。

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

相关问题 如何从构造函数参数初始化模板成员数组? - How to initialize template member array from constructor parameter? 如何使用std :: array构造函数参数C ++列表初始化const std :: array成员 - How to list-initialize a const std::array member using a std::array constructor argument C++ 如何通过构造函数通过成员初始化列表初始化C风格的char数组和int数组? - How to initialize C-style char array and int array via member initialization list through constructor? 为什么 std::array 需要大小作为模板参数而不是构造函数参数? - Why does std::array require the size as a template parameter and not as a constructor parameter? 零初始化初始化列表中的数组成员 - Zero-Initialize array member in initialization list 如何使用成员初始化列表初始化数组? - How can i use member initialization list to initialize an array? 在构造函数的初始化列表中初始化数组 - Initialize array in constructor's initialization list 在成员初始化列表中填写std :: array - fill std::array in the member initialization list 如何为std :: array的列表初始化构造函数编写包装器? - How to write a wrapper for std::array's list initialization constructor? 如何通过构造函数确定私有std :: array成员的大小? - How can I size a private std::array member through the constructor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM