From 8dcb630ad5b8d2da9a324ef023073442b046a5e1 Mon Sep 17 00:00:00 2001 From: Francesco Biscani Date: Fri, 20 Sep 2024 22:22:15 +0200 Subject: [PATCH] Tweaks. --- include/heyoka/detail/rng_to_vec.hpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/include/heyoka/detail/rng_to_vec.hpp b/include/heyoka/detail/rng_to_vec.hpp index 21b5e9c2a..40fb91d26 100644 --- a/include/heyoka/detail/rng_to_vec.hpp +++ b/include/heyoka/detail/rng_to_vec.hpp @@ -9,6 +9,7 @@ #ifndef HEYOKA_DETAIL_RNG_TO_VEC_HPP #define HEYOKA_DETAIL_RNG_TO_VEC_HPP +#include #include #include #include @@ -23,16 +24,28 @@ namespace detail // Helper to convert an input range R into a vector. // The value type is deduced from the reference type of R. +// +// NOTE: the need for this helper - as opposed to using directly +// the std::vector ctor from iterators - arises because the std::vector +// ctor requires C++17 input iterators, but C++20 input iterators +// are not necessarily C++17 input iterators. Thus, we go through +// a range-based for loop in which we push_back() the elements from +// the input range instead. template auto rng_to_vec(R &&r) { + static_assert(std::ranges::input_range); + // Deduce the value type. using value_type = std::remove_cvref_t>; // Prepare the return value, reserving the appropriate - // size if R is a sized range. + // size if R is a sized range whose size type is an integral type. std::vector retval; - if constexpr (std::ranges::sized_range) { + if constexpr (requires { + requires std::ranges::sized_range; + requires std::integral>; + }) { retval.reserve(static_cast(std::ranges::size(r))); }