How to deal with static storage duration warnings?

One way to defer initialization of global variables such as the ones you are using is to wrap them in get-functions.

std::default_random_engine& getEngine()
{
   // Initialized upon first call to the function.
   static std::default_random_engine engine(static_cast<unsigned int>(time(nullptr)));
   return engine;
}

std::uniform_int_distribution<unsigned int>& getRandomInt()
{ 
   // Initialized upon first call to the function.
   static std::uniform_int_distribution<unsigned int> randomInt(1, 6);
   return randomInt;
}

and then use getEngine() and getRandomInt() instead of using the variables directly.

Leave a Comment

tech