I have several large arrays of 64-bit integers whose values are expensive to calculate, so I presently have generator codes in my build system that calculate the constants and generate the .cpp files to initialize the arrays. Some of them are 1d, but many of them are 2d. Previously, my generator scripts have declared them thus (the values are calculated by my generator code)
namespace common{ namespace bitboards{
///// Additional stuff
const bitboard ray_thr_sqs[64][64] = {
{0x81412111090503fe, 0xfc, 0xfa, 0xf6, 0xee,......
};};
but I want to use to do it in a more C++ without C like arrays. (A few of them are bigger than this with size[4096][64]). For the 1d arrays, I can just use std::array. For the 2d ones, I don't want to use nested std::arrays for performance reasons, but would rather use boost multi_array.
How can I define a const boost multi_array? I have tried this:
//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,......
};};
where makeMultiArray is a helper function to generate the arrays and bitboard is a typedef for uint64_t. (This helper function takes a flat initializer list) This compiles, but when I come to run it, the array is empty. I know why it is empty, and it's pretty obvious after the fact: makeMultiArray is not being run, but I can't think of another way to do what I want without staying with C like arrays or nested std::arrays.
Is there a way to do this with constexpr or something, or a better way of doing it entirely without using my generator code.
edit: These are declared at namespace scope in a .cpp file.
edit 2 A commenter below now suspects a linking problem, rather than what I initially suspected, and I now agree. I have tried a minimal working example with the makeMultiArray function above, and it does work, so the problem is elsewhere. I have added the names of the namespaces in the above. Also, the declaration in the header file is thus:
//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;
};};
Here is file2.cpp
namespace common{ namespace bitboards{
//////Additional stuff
const std::array<bitboard, 8> ranks = {0xff, 0xff00, 0xff0000, 0xff000000,
0xff00000000, 0xff0000000000, 0xff000000000000, 0xff00000000000000};
};};
Could the definition being in two separate cpp files be causing my issue?