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
7
votes
2 answers

Why can't I use istream_view and std::accumulate to sum up my input?

This program: #include #include #include int main() { auto rng = std::ranges::istream_view(std::cin); std::cout << std::accumulate(std::ranges::begin(rng), std::ranges::end(rng), 0); } is supposed to sum…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
6
votes
1 answer

Is it still impossible to sort a range with proxy iterators whose value_type/reference is pair in C++23?

Although P2321 zip provides the common_reference specialization for pair, and P2165 Compatibility between tuple, pair and tuple-like objects makes tuple and pair interconvertible, the comparison function of pair only has the following candidates in…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
1 answer

Is there a way to determine if a range is constructed correctly?

I encountered unexpected behavior when mixing the output of boost::join with std::views::transform (shown below). The compiler issues no warnings whatsoever. Fortunately, an address sanitizer detects an invalid memory access in get2b(). The…
MarkB
  • 672
  • 2
  • 9
6
votes
1 answer

Using C++20 Ranges to Avoid Loops

I was assigned a task where I need to solve a problem given several constraints. The point is to enforce the use of STL algorithms, iterators, and new c++20 functionality including things like ranges. However I've been reading on ranges for hours…
Adham
  • 121
  • 7
6
votes
0 answers

Is there a fundamental performance cost to using std::views::join?

I've recently been doing some performance evaluation of ranges & views. I've posted a simple example (also at https://www.godbolt.org/z/7ThxjKafc) where the difference in assembly is much more significant than I would have expected. With latest GCC…
MarkB
  • 672
  • 2
  • 9
6
votes
1 answer

why I can't use a take() after a istream_view in c++20

The code shows my problem, I can't use take(3) after istream_view. The error message is: /home/linuxbrew/.linuxbrew/Cellar/gcc/11.1.0_1/include/c++/11.1.0/ranges:1775:48: error: passing…
Jiangqiu Shen
  • 81
  • 1
  • 4
6
votes
2 answers

ranges::view::transform produces an InputIterator preventing the use of std::prev

Consider the following code, which uses the Ranges library from C++20: #include #include #include int main() { std::vector v{0,1,2,3,4,5,6,7}; auto transformed = std::ranges::views::transform(v, [](int i){…
lisyarus
  • 15,025
  • 3
  • 43
  • 68
6
votes
2 answers

Why is ranges::basic_istream_view::begin() not cached?

I found that c++20 ranges::basic_istream_view is slightly different from the range-v3 version. The most important difference is that the std::ranges::basic_istream_view does not cache its begin(), so that each begin()s will return the next iterator…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
1 answer

How to support range adaptors in custom container?

I have created a custom container called goldbox that only contains arithmetic types and I have also implemented begin and end member functions to iterate over the elements. My Full Source Code: #include #include #include…
Desmond Gold
  • 1,517
  • 1
  • 7
  • 19
6
votes
1 answer

Generator called twice in C++20 views pipeline

Here in a simple pipeline of views adaptors, there is the gen function called to generate a sequence of values (using an internal state) and then a filter on it. What is surprising and counterintuitive (at least for me) is the fact that the…
Pascal H.
  • 1,431
  • 8
  • 18
6
votes
0 answers

C++20 views::join goes into an infinite loop on generated nested ranges::single_view

I'm playing with C++20 ranges with GCC implementations (v10.2 & v11). To test the behavior of std::views::join, I tried to generate a nested view using single and then I flat it using join. #include #include int main() { auto…
Pascal H.
  • 1,431
  • 8
  • 18
6
votes
1 answer

Why can views::reverse transform a non-sized_range into a size_range?

In [range.sized#1]: The sized_­range concept refines range with the requirement that the number of elements in the range can be determined in amortized constant time using ranges​::​size. template concept sized_­range = range && …
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
1 answer

std::ranges::copy will not accept std::vector in visual studio

The code below does not compile in Visual Studio, giving Error C2672 'operator __surrogate_func': no matching overloaded function found sortms C:\Users\David\source\repos\sortms\sortms.cpp 103 The function works as written when I use a C-style…
davidbear
  • 375
  • 2
  • 13
6
votes
1 answer

what are the constraints for std::ranges::make_heap?

The following code works well with gcc: struct S { int i, j; auto operator<(const S& s) const { return i < s.i; }; }; std::vector v; std::make_heap(v.begin(), v.end()); But when I switch to C++20's range…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
6
votes
1 answer

How to use ranges::sort for ascending or descending sort controlled by a boolean

Using ranges allowed me to reduce boilerplate, so it's great but I could not find a way sort either in ascending order or descending order. The following snippet compiles just fine (g++ 10.2.0) and projection really simplifies the code, no need for…
user3636086
  • 803
  • 1
  • 7
  • 10