我有几个 64 位整数的大型数组,它们的值计算起来很昂贵,所以我目前在我的构建系统中有生成器代码,用于计算常量并生成 .cpp 文件来初始化数组。其中一些是 1d,但其中许多是 2d。以前,我的生成器脚本已这样声明它们(值由我的生成器代码计算)
namespace common{ namespace bitboards{
///// Additional stuff
const bitboard ray_thr_sqs[64][64] = {
{0x81412111090503fe, 0xfc, 0xfa, 0xf6, 0xee,......
};};
但我想在没有 C 类数组的情况下使用更多的 C++ 来完成它。(其中一些比这更大,大小为 [4096][64])。对于一维数组,我可以只使用 std::array。对于二维的,出于性能原因,我不想使用嵌套的 std::arrays,而是宁愿使用 boost multi_array。
如何定义 const boost multi_array?我试过这个:
//file1.cpp
namespace common{ namespace bitboards{
///////additional stuff
const boost::multi_array<bitboard, 2> ray_thr_sqs = misc::boost_help::makeMultiArray<bitboard, 64, 64>({0x81412111090503fe, 0xfc, 0xfa, 0xf6, 0xee,......
};};
其中 makeMultiArray 是生成数组的辅助函数,而 bitboard 是 uint64_t 的 typedef。(这个辅助函数需要一个平面初始化列表)这可以编译,但是当我运行它时,数组是空的。我知道为什么它是空的,而且事后很明显:makeMultiArray 没有运行,但我想不出另一种方法来做我想做的事,而不使用 C 之类的数组或嵌套的 std::arrays。
有没有办法用 constexpr 或其他东西来做到这一点,或者有更好的方法完全不使用我的生成器代码。
编辑:这些是在 .cpp 文件的命名空间范围内声明的。
编辑 2下面的评论者现在怀疑存在链接问题,而不是我最初怀疑的问题,我现在同意。我用上面的 makeMultiArray 函数尝试了一个最小的工作示例,它确实有效,所以问题出在其他地方。我在上面添加了命名空间的名称。此外,头文件中的声明如下:
//header.hpp
namespace common{ namespace bitboards{
//This const is definied in generated file1.cpp above which I generate in my build scripts because the are large arrays and expensive to calculate
extern const boost::multi_array<bitboard,2> ray_thr_sqs;
//This is another constant array that is much smaller and I can write by hand in a second cpp file, file2.cpp
extern const std::array<bitboard, 8> ranks;
};};
这是file2.cpp
namespace common{ namespace bitboards{
//////Additional stuff
const std::array<bitboard, 8> ranks = {0xff, 0xff00, 0xff0000, 0xff000000,
0xff00000000, 0xff0000000000, 0xff000000000000, 0xff00000000000000};
};};
两个单独的 cpp 文件中的定义是否会导致我的问题?