I was wondering if there is some tool in C++ that I don't know yet that can minimize the scope of my objects and functions in the following simple example where I fill a vector with random dice rolls:
std::vector<int> dice_samples(10);
std::mt19937_64 m_gen;
std::uniform_int_distribution<int> dist(1, 6);
std::generate(dice_sample.begin(), dice_sample.end(), [&](){
return dist(m_gen);
});
Ideally, I want both the Generator and Distribution to be scoped within the generate call. I could move their construction into the lambda expression but something like this would seem extremely inefficient as I would be constructing a Generator and Distribution on each call:
std::vector<int> dice_samples(10);
std::generate(dice_sample.begin(), dice_sample.end(), [](){
std::mt19937_64 m_gen;
std::uniform_int_distribution<int> dist(1, 6);
return dist(m_gen);
});
In other words is there a way to scope the construction and destruction of a object once within a function call or lambda expression? Or is this not yet possible in the latest version of C++?