gpt4 book ai didi

c++ - 如何使用自定义值初始化模板类中的数组?

转载 作者:行者123 更新时间:2023-11-30 03:40:54 25 4
gpt4 key购买 nike

例如

template<size_t N>
class A
{
array<int, N> m;
static A const UNIT {1, 1, ...}; // repeated N times,
// but I can't because of currently unspecified N
}

如何使用自定义值 1 初始化模板大小的数组?

最佳答案

您可以使用填充功能。这也适用于静态常量成员。

template<size_t N>
class A {
array<int, N> m;
public:
static A const unit;
A() { m.fill(1); }
};

template<size_t N>
A<N> const A<N>::unit{};

关于c++ - 如何使用自定义值初始化模板类中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37737624/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com