从另一个静态常量数组初始化静态常量数组(只需少量计算)

Initialize Static const array from another static const array (With a small computation)

本文关键字:数组 静态 常量 计算 另一个 初始化      更新时间:2023-10-16

所以我有一堆无符号类型的静态常量数组。我在标头中声明它们并在 cpp 文件中初始化它们。阵列非常大(它们是基于 OFDM 的发射器的载波索引(。但是,我有文档中的值。因此,只需复制粘贴即可初始化。到目前为止,这工作正常。

但是我需要第二组数组,它们只不过是原始集合+一个常量值。

在页眉中

class C
{
static const uint32_t A[288];
static const uint32_t A_ext[288];
}

在CPP中

const uint32_t C::A[288] = {1,2,3......};

我希望A_ext成为

A_ext[i] = A[i] + 5;

我希望这些也定义为静态 const,因为所有这些数组都只能在项目的其他任何地方读取。它们就像标准表,可以在项目的其他任何地方访问。

我该怎么做?

我会避免相互依赖的静态初始化。您可以使用

struct carrier_indices_t {
uint32_t A[288];
uint32_t A_ext[288];
};
class C {
static const carrier_indices_t carrier_indices;        
}

然后

const carrier_indices_t C::carrier_indices = foo();

其中foo是返回carrier_indices_tconstexpr函数。

或者,仅存储其中一个数组并提供两种静态方法来访问它(一种偏移量为+5(。