初始化const boost multi_array

我有一些大型的64位整数数组,其值计算起来很昂贵,因此我目前在生成系统中有生成器代码,用于计算常量并生成.cpp文件以初始化数组。其中一些是1d,但许多是2d。以前,我的生成器脚本已经声明了它们(值由我的生成器代码计算)

   namespace common{ namespace bitboards{

   /////    Additional stuff

   const bitboard ray_thr_sqs[64][64] = {
                     {0x81412111090503fe,0xfc,0xfa,0xf6,0xee,......

   };};

但是我想在没有C类数组的C ++中使用它。 (其中一些比size [4096] [64]大)。对于一维数组,我可以只使用std :: array。对于2D模型,出于性能原因,我不想使用嵌套的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,......

    };};

其中makeMultiArray是用于生成数组的辅助函数,而位板是uint64_t的typedef。 (此辅助函数采用一个平面的初始化程序列表)可以编译,但是当我开始运行它时,该数组为空。我知道为什么它是空的,并且在事实之后很明显:makeMultiArray没有运行,但是我想不出另一种方法来做我想要的事情,而不用像数组或嵌套的std :: arrays这样的C。 >

有没有办法用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文件中的定义会引起我的问​​题吗?

qs1012 回答:初始化const boost multi_array

事实证明,这是其他地方的错误。在使用数组的某一点上,索引计算出现错误,导致其超出范围。我从boost :: multi_array得到的冗长错误导致我怀疑该数组完全未初始化,而实际上这是一个超出范围的问题。感谢您的帮助。

本文链接:https://www.f2er.com/3120688.html

大家都在问