Here is an algorithm std::shuffle
from https://en.cppreference.com/w/cpp/algorithm/random_shuffle:
template<class RandomIt, class URBG> void shuffle(RandomIt first, RandomIt last, URBG&& g) { typedef typename std::iterator_traits<RandomIt>::difference_type diff_t; typedef std::uniform_int_distribution<diff_t> distr_t; typedef typename distr_t::param_type param_t; distr_t D; diff_t n = last - first; for (diff_t i = n-1; i > 0; --i) { using std::swap; swap(first[i], first[D(g, param_t(0, i))]); } }
Why the algorithm takes a forwarding reference to the generator
URGB&& g
? As long as it didn't usestd::forward<URGB>(g)
to forward the generator either as an l-value or as an r-value?Why the using declaration is inside of loop body rather than outside of it? Does leaving it inside (iteratively) affects performance? Thank you!