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
13
votes
1 answer

What is the difference between iterator_category and iterator_concept in C++20?

C++20 brings a more powerful iterator system, one of them is to introduce iterator_concept on the basis of iterator_category. I found that the iterator_concept and iterator_category of many iterators in C++20 are inconsistent. Take the most famous…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
13
votes
1 answer

C++20 ranges and sorting

I'm dealing with the last big 4 of C++ 20, attempting to learn the new main features. Trying some code from the web related to ranges, I've written: std::vector ints{ 6, 5, 2, 8 }; auto even = [](int i) { return 0 == i % 2; }; // ranges... auto…
Chris
  • 277
  • 4
  • 15
13
votes
2 answers

Can you zip with the new ranges library?

See: http://eel.is/c++draft/#ranges Given two C++2a ranges (as in objects that conform to the ranges concept of the ranges library) a and b, of equal length, is there a way to zip them together such that: for (const auto& [a,b] : zip(a,b)) does…
Andrew Tomazos
  • 66,139
  • 40
  • 186
  • 319
12
votes
1 answer

Tweaking clang-format for C++20 ranges pipelines

C++20 (and 23 with std::ranges::to()) makes idiomatic the use of operator| to make a pipeline of transformations such as this: return numbers | std::views::filter([](int n) { return n % 2 == 0; }) |…
Ben
  • 9,184
  • 1
  • 43
  • 56
12
votes
1 answer

How you create your own views that interact with existing views with operator |?

Why does this code work with the #if 0 block in place, but fails with a fairly complex set of error messages if you remove it? And more importantly, how do I make it the same result as the very similar block above it? #include #include…
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
12
votes
2 answers

Why was std::ranges::less introduced?

On cppreference on std::ranges::less, in notes we can see that: Unlike std::less, std::ranges::less requires all six comparison operators <, <=, >, >=, == and != to be valid (via the totally_ordered_with constraint). But... why? Why would we use…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
12
votes
1 answer

Should C++20 std::ranges::sort not need to support std::vector?

I noticed that std::ranges::sort cannot sort std::vector: :6:51: error: no match for call to '(const std::ranges::__sort_fn) (std::vector >)' 6 | std::ranges::sort(std::vector{false, true, true}); | …
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
11
votes
2 answers

C++20's std::views::filter not filtering the view correctly

I was writing a simple C++ program that generates a list of random integer values from a normal distribution, then takes first N generated items and filters them so that their absolute value is greater than 42. The code i wrote is the following: int…
11
votes
2 answers

Bug or compilation error with some compilers for simple std::ranges code

I have a piece of code that uses the ranges library of C++20, taken from this SO anwer. The code is rejected by some compiler (versions) and some older GCC versions return garbage. Which compiler is right? The code is supposed to print the elements…
joergbrech
  • 2,056
  • 1
  • 5
  • 17
11
votes
1 answer

Are view iterators valid beyond the lifetime of the view?

Say I have a custom container class that stores data in a map: class Container { public: void add(int key, std::string value) { _data.emplace(key, std::move(value)); } private: std::map _data; }; I want to provide an…
Kevin
  • 6,993
  • 1
  • 15
  • 24
11
votes
3 answers

Why is std::common_iterator just std::forward_iterator?

C++20 introduced a std::common_iterator that is capable of representing a non-common range of elements (where the types of the iterator and sentinel differ) as a common range (where they are the same), its synopsis defines…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
11
votes
1 answer

Use of 'auto [...] 'before deduction of 'auto' with recursive, concept-based function template

I wanted to create a deep_flatten function template that would produce a range of elements that are deeply joined. For example, if we take into account only nested std::vectors, I can have: template struct is_vector : public…
Fureeish
  • 12,533
  • 4
  • 32
  • 62
10
votes
1 answer

Why does the C++23 ranges adaptor require a callable object to be copy_­constructible?

Some ranges adaptors such as filter_­view, take_­while_­view and transform_view use std::optional's cousin copyable-box to store the callable object: template class transform_view : public…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
10
votes
2 answers

What should I do to make my container work with ranges?

I have a simple container: template > class ring { public: using value_type = T; using allocator_type = Allocator; using size_type = std::size_t; using difference_type = std::ptrdiff_t; …
Dmitriano
  • 1,878
  • 13
  • 29
10
votes
2 answers

Should c++ constraints be evaluated eagerly or lazily?

The main purpose of this question is to draw community's attention to libstdc++ ranges not working with clang: https://bugs.llvm.org/show_bug.cgi?id=46746 Avi Kivity suggested this is a gcc bug: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97120 But…
Mikhail
  • 20,685
  • 7
  • 70
  • 146
1
2
3
25 26