Skip to main content
1 of 3
user avatar
user avatar

Best way to structure multiple constants C++

Context

I'm developing a chess engine of my own, where if anything can be pre-initialized/pre-calculated, should be. This is because speed is the number-1 priority, and every extra second you spend calculating stuff that could've been calculated before-hand, stacks up and ultimately makes you lose a lot of important time that should have been used for things that cannot be pre-calculated, like evaluation.

The main pre-initialized variables are the various Bitboard masks.

  • Diagonals
  • Ranks
  • Files
  • Different square patterns ( center ,edges, light squares, dark squares )
  • Attacks for pieces that can be pre-calculated - Knight, King, Pawns

It also has some constepxr variables.
I have a separate header file that manages all of these. They can be initialized once in the start and then used throughout the program several times.

Question

What I am currently doing is using nested namespaces

namespace Mask 
{
    namespace File
    {
        constexpr uint64_t FileA = ...;
        ...
        extern uint64_t Files[NB_SQ] 
    }
    
    namespace Rank{...}

    namespace Attack{...}
    ...
}

But I am really not happy with this, it gets very messy to know which mask can belong where. Moreover, this isn't the purpose of namespaces, Is there a better way I can structure this?

user374476