Signature of take is
template< ranges::viewable_range R, class DifferenceType >
requires /* ... */
constexpr ranges::view auto take( R&& r, DifferenceType&& count );
It is a minor thing but I wonder why DifferenceType
is not some ssize type(practically int64_t
on modern machines).
Is this just to avoid warnings on comparisons of integers of different signednes, or is there some other design reason I am missing. My intuition that having a fixed type would make error messages easier to read, so I wonder what was the motivation.
If somebody is interested in code example, here it is, it feels a bit artificial since I wanted to keep it short, but it is quite possible to pass wrong type to take argument and then error message is unreadable(my favorite part is:
/opt/compiler-explorer/gcc-trunk-20220401/include/c++/12.0.1/bits/atomic_base.h:98:3: note: candidate: 'constexpr std::memory_order std::operator|(memory_order, __memory_order_modifier)'
).
#include <vector>
#include <iostream>
#include <ranges>
#include <string>
#include <fmt/format.h>
#include <fmt/ranges.h>
auto get_n(){
return std::string("2");
}
int main() {
std::vector v {2,3,5,7,11};
const auto n = get_n(); //oops this is std::string
auto dont_even = v | std::views::filter([](const int& i){return i%2==1;}) | std::views::take(n);
std::cout << fmt::format("{}", dont_even);
}