Questions tagged [std-ranges]

A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from header and of range adaptors divided into views and actions.

379 questions
5
votes
1 answer

Get rid of nested for loops with std::ranges

Let I have a code: for (auto& a : x.as) { for (auto& b : a.bs) { for (auto& c : b.cs) { for (auto& d : c.ds) { if (d.e == ..) { return ... …
w00drow
  • 468
  • 2
  • 11
5
votes
1 answer

C++20 std::ranges: Range adapter to skip every nth element

I'm trying to get more acquainted with C++20's std::ranges implementation, and I've come across a seemingly simple problem of which I cannot find a standard solution without rolling my own implementation. The problem is simple: I would like to only…
Human-Compiler
  • 11,022
  • 1
  • 32
  • 59
5
votes
1 answer

What is the purpose of (void) casts in the GCC standard library implementation?

In the implementation of std::ranges::transform in GCC's stdlibc++, why does the for loop iterator increment have a (void) cast? for (; __first1 != __last1 && __first2 != __last2; ++__first1, (void)++__first2, ++__result)
5
votes
0 answers

Under what circumstances does the iterator of ranges::split_view not satisfy copyable?

C++20 introduces a ranges::split_view, which accepts a view and a delimiter, and splits the view into subranges according to the delimiter. The delimiter is also a view, and when it is a single element, it will be wrapped in a…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
5
votes
1 answer

Why does views::reverse not work with iota_view

I have the following C++ program, and for some reason I can not use int64_t as template argument. #include #include template void fn() { for (auto val : std::ranges::iota_view{T{1701}, T{8473}} …
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
5
votes
1 answer

std::lexicographical_compare_three_way for ranges

Why is there std::lexicographical_compare_three_way, but no std::ranges::lexicographical_compare_three_way? There is argument Comp in std::ranges::lexicographical_compare, but it is rather useless, because function returns bool, when one of the…
SherAndrei
  • 528
  • 3
  • 14
5
votes
1 answer

Range-v3 view composition and views calculation parallelization

Taken from range-v3 docs, the following example demonstrates a simple composition of views pipelined to produce a range: std::vector const vi{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; using namespace ranges; auto rng = vi | views::remove_if([](int…
gonidelis
  • 885
  • 10
  • 32
5
votes
1 answer

C++20: How to split range by size?

I want to split range {1, 2, 3, 4, 5} to range of subranges of (e.g with size of 2: {{1, 2}, {3, 4}, {5}}). Yet std::views::split only splits by delimiter. Is there no standard "reverse join" or something to do this?
gavrilikhin.d
  • 554
  • 1
  • 7
  • 20
5
votes
1 answer

why can't I call `ranges::begin` on a `const filter_view`?

I can't call ranges::begin on a const filter_view https://en.cppreference.com/w/cpp/ranges/filter_view the begin and end doesn't seem to be const. why is that? int main(){ std::vector v{1,2,3}; // removing const will make it compile …
Hui
  • 571
  • 1
  • 3
  • 9
5
votes
2 answers

C++20 Streams aka Ranges

When I use the Stream Library (http://jscheiny.github.io/Streams/api.html#) I can do similar things like in Java-Streams: #include "Streams/source/Stream.h" #include using namespace std; using namespace stream; using namespace…
Matthias
  • 135
  • 10
5
votes
1 answer

What are the rules for creating your own (pipeable) ranges ::views and ::actions?

In this answer, we can see that we can create our own views. I have tried that: template struct squared_view : std::ranges::view_interface> { struct iterator; constexpr squared_view() = default; …
Fureeish
  • 12,533
  • 4
  • 32
  • 62
4
votes
1 answer

Why can't `ranges::begin` be called on a `const any_view`?

I have this code that fails (Explorer): #include #include int main() { std::vector a{1}; const ranges::any_view av(a); ranges::begin(av); } And was in the…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
4
votes
1 answer

Does std::ranges::views::enumerate use the wrong type for indexing on GCC?

First, using Range-v3, I noticed that std::vector v; auto w = v | ranges::views::enumerate; auto [x, y] = *w.begin(); static_assert(std::is_same_v); which makes sense to me because std::size_t is the right type to use…
Enlico
  • 23,259
  • 6
  • 48
  • 102
4
votes
1 answer

Is there a way to test that a std::ranges::views iterator is the end iterator just with the iterator?

In the following function namespace r = std::ranges; namespace rv = std::ranges::views; std::optional find_first_adjacent_duplicate(std::span vals) { auto pairs = vals | rv::slide(2); auto adj_dup = r::find_if(pairs, [](auto&& r)…
jwezorek
  • 8,592
  • 1
  • 29
  • 46
4
votes
4 answers

using ranges to filter side by side vectors while remaining readable

So imagine i have 2 vectors (for reasons to do with the framework i am working in): std::vector valid = {false, true, true, false}; std::vector percentages = {2, 50, 3, 100}; These vectors represent a batteries validity and the…
Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175