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
1
vote
1 answer

Range transformations with stateful lambdas and std::views::drop

it's my first time digging into the new library and I tried a little experiment combining std::views::transform with a stateful lambda and 'piping' the resulting range to std::views::drop: #include #include #include…
theejazz
  • 41
  • 5
1
vote
0 answers

std::ranges::sort doesn't use hidden friend std::iter_swap

I'm writing a proxy iterator using Boost.STLInterfaces. It has inline friend constexpr void iter_swap(iter lhs, iter rhs) noexcept. ranges::sort requires, among others, std::indirectly_swappable, which requires ranges::iter_swap for the iterator.…
SD57
  • 73
  • 7
1
vote
1 answer

Using two different copies of an identical std::view

I'm trying to use views in a commercial application, and noticed an inconsistency between gcc and Visual Studio. In the code below, calling transformed() twice returns two different, apparently incompatible views. In gcc 11 (on godbolt), the code…
Rob L
  • 2,351
  • 13
  • 23
1
vote
1 answer

What is the status of std::ranges::split_view in C++20?

According to https://en.cppreference.com/w/cpp/ranges/split_view, std::ranges::split_view must be available since C++20. However the example on the same page contains "C++23" in its text: #include #include #include…
Fedor
  • 17,146
  • 13
  • 40
  • 131
1
vote
1 answer

How to use std::ranges on a vector for a function that needs two arguments?

I have been trying to understand the new ranges library and try to convert some of the more traditional for loops into functional code. The example code given by cppreference is very straight forward and readable. However, I am unsure how to apply…
Sailanarmo
  • 1,139
  • 15
  • 41
1
vote
0 answers

How to apply std::ranges::min on a subrange?

I'm trying to wrap my head around how ranges work. The following code seems natural to me: #include #include #include int main() { std::multimap a{ { 1, 2 }, { 1, 3 }, { 4, 5 }, …
Jupiter
  • 1,421
  • 2
  • 12
  • 31
1
vote
0 answers

Using ranges::copy_if versus using iterator constructor with views::filter

I noticed that I can seemingly get the same logic(copy elements matching some predicate into vector) both using ranges::copy_if and also using vector constructor taking 2 iterators(by providing it with filter_view .begin() and .end()). #include…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
1 answer

Why does std::filter_view not have a way to eagerly evaluate it and convert it to view whose begin in const?

filter_view does not have a const begin. But I wonder why isn't there a member or free function on it that would return some_filter_view type that is that is same as filter_view except it has a const begin(). Obviously this method would be O(n),…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
1 answer

How to convert std::views::join result to string_view at compile time?

I want to make the whole code work as constexpr. Here's what works: #include #include #include int main() { constexpr std::string_view words{"Just some sentence I got from a friend."}; auto rng = words |…
Mr.WorshipMe
  • 713
  • 4
  • 16
1
vote
0 answers

joining std::ranges::single_views unexpected results

I'm trying to understand some basic constructions with std::ranges like the Pythagorean triples examples of Niebler's blog. However, when I try the following baby example #include #include template auto…
Reimundo Heluani
  • 918
  • 9
  • 18
1
vote
1 answer

std::multimap equal _range and C++20 std::views::values do not work nicely together

I have the following code, it works, but C++20 version does not look much better than C++17 version. My guess issue is that multimap equal_range returns a pair and ranges can not figure out that is a pair of valid iterators. Is there a way to write…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
1
vote
1 answer

How to properly define is-derived-from-view-interface?

LWG3549 proposes that view_interface does not need to inherit view_base, which allows range adaptors to better perform empty base optimization. In the latest [range.view], the definition of view concept has undergone the following…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
1
vote
1 answer

Generic programming with ranges/views not templates?

I'd like to have function that accepts any container of a fixed type. For a example a function that will accept both std::array and std::array. I thought this would be possible with ranges but I'm realizing my understanding is…
Tom Huntington
  • 2,260
  • 10
  • 20
1
vote
2 answers

What's the difference between std::ranges::swap() and std::swap()?

In C++20, there are two swap function templates: std::ranges::swap(T&&, U&&) and std::swap(T&, T&). I just wonder: What's the difference between they two?
xmllmx
  • 39,765
  • 26
  • 162
  • 323
1
vote
0 answers

C++ Drop Sub-range from the middle of a range

I have a container (std::vector), and I'd like to check if any of its elements satisfy a predicate, to do that I use C++20's std::ranges::any_of. The problem is, I'd like this any_of to skip cetain elements in the middle of the range (a sub-range…
aradarbel10
  • 435
  • 3
  • 10