Fix random::uniform_from to work with rvalue containers and return types

This commit is contained in:
Nikita Lisitsa 2023-02-08 23:56:23 +03:00
parent 1f1612f7e6
commit e304b09f9f

View file

@ -56,9 +56,9 @@ namespace psemek::random
} }
template <typename RNG, typename Container> template <typename RNG, typename Container>
auto & uniform_from(RNG && rng, Container & container) decltype(auto) uniform_from(RNG && rng, Container && container)
{ {
if (container.empty()) if (container.size() == 0)
throw std::runtime_error("cannot sample from empty container"); throw std::runtime_error("cannot sample from empty container");
return *std::next(container.begin(), uniform<std::size_t>(rng, 0, container.size() - 1)); return *std::next(container.begin(), uniform<std::size_t>(rng, 0, container.size() - 1));
} }