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
4
votes
3 answers

Is it possible to use a dynamic number of range adaptors?

I am fairly new to ranges, and I wanted to know if there was a way to apply a dynamic number of range adaptors. I have fiddled around with some code for a while, and I have also done some searching, but to no avail. #include #include…
Jack Casey
  • 65
  • 1
  • 4
4
votes
1 answer

How do I join views using std::ranges

How do I join two views using std::ranges? In ranges-v3 the views are joined with views::concat(), and I cannot figure out how to do that with std::ranges. #include #include #include #include using namespace…
Generic Name
  • 1,083
  • 1
  • 12
  • 19
4
votes
1 answer

Why does std::get only have two function overloads for ranges::subrange?

There are four pair-like types in the standard, namely std::array, std::pair, std::tuple, and ranges::subrange, where the overload of std::get for ranges::subrange is defined in [range.subrange#access-10]: template
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
2 answers

Why can't I insert this transformed directory_iterator into a vector?

I am trying to insert a transformed range of directory entries into a vector using its insert(const_iterator pos, InputIt first, InputIt last) member function template. Unfortunately I can't get the following code to compile under GCC 11.1.0, which…
neop
  • 89
  • 2
  • 9
4
votes
1 answer

What is the meaning of default_constructible range adaptors in C++23?

Since C++23, views are no longer required to be default_constructible. For range adaptors such as views::filter and views::transform, their default constructor is redefined as: template>…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
1 answer

Why does the constructor of ranges::iota_view not move the argument to member variable?

In [range.iota.view], the well-known constructor of iota_view is defined as follows (emphasis mine): constexpr explicit iota_view(W value); Preconditions: Bound denotes unreachable_­sentinel_­t or Bound() is reachable from value. Effects:…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
3 answers

Can trim of a string be done inplace with C++20 ranges?

Inspired by cute cppreference example of trim with C++20 I have written the following code(I have changed the return type to void and arg to std::string& since my "problem"(I am inventing problems to learn C++20) does not exist in original code that…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
4
votes
1 answer

Why the return view of C++20 range adaptors is not a constant expression?

Consider the following code: #include int main() { constexpr int a[] = {1, 2, 3, 4}; constexpr auto r = a | std::views::take(3); static_assert(*r.begin() == 1); } msvc accept it, gcc rejects it with: :5:44: error:…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
1 answer

Usefulness of post-incrementing a weakly_incrementable?

I studied the weakly_incrementable concept recently and I am wondering what the post-increment operator is good for, given that the concept does not define any requirements for the result of the operation. Is it actually okay to return void for…
Markus Mayr
  • 4,038
  • 1
  • 20
  • 42
4
votes
1 answer

Why can't std::ranges::views::filter take std::isupper as argument?

Consider the following code: #include #include constexpr inline auto filter_upper = std::views::filter(::isupper); The new range adaptors filter_upper works fine with global old c-function ::isupper, but if I replace with…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
1 answer

Joining a range of strings with a delimiter using standard ranges

I want to convert four bytes contained in a span into a string using ranges. Here's an example of input and output: std::span data{bytes}; // contains 0x11, 0x22, 0x33, 0x44 std::string result = ...; // results in 44:33:22:11 Here's…
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
4
votes
1 answer

std::ranges::sort is not usable as a 'constexpr' function when saving its return value in lambda function

This function f can take C++20 ranges algorithm objects as an argument then use it: constexpr auto f(auto algo) { return [=] { algo(std::array{1, 0}); return true; }(); } and it works fine with…
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
4
votes
0 answers

Why didn't C++20 provide range/constrained versions of algorithms from ?

According to cppreference, all numeric algorithms under header didn't get ranges versions in C++20. Are there reasons?
康桓瑋
  • 33,481
  • 5
  • 40
  • 90
3
votes
0 answers

No member named 'split' in namespace 'std::ranges::views'

I'm getting this error on macOS, with clang, using std=c++20 or std=c++2b . error: no member named 'split' in namespace 'std::ranges::views' auto tokensRanges = blob | std::views::split('.'); Compiler details: Apple clang version 14.0.3…
yonutix
  • 1,964
  • 1
  • 22
  • 51
3
votes
1 answer

Why is passing rvalue range that is not viewable range to range adaptor allowed in latest compiler?

I am learning about ranges and as I learned about range adaptors, they only accept viewable_range as the first argument according to cppreference RangeAdaptorObject. But when I tested this code on Compiler Explorer: auto val =…
Waker
  • 216
  • 9