A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from
Questions tagged [std-ranges]
379 questions
45
votes
2 answers
Why must a std::ranges::filter_view object be non-const for querying its elements?
#include
#include
#include
using namespace std::literals;
int main()
{
auto fn_is_l = [](auto const c) { return c == 'l'; };
{
auto v = "hello"sv | std::views::filter(fn_is_l);
std::cout…

xmllmx
- 39,765
- 26
- 162
- 323
40
votes
2 answers
C++20 ranges too many | operators?
I am using g++ 10.2 for this code. Does anybody know why I get a compiler error for the last std::views::reverse on results3?
#include
#include
int main() {
auto values = std::vector{1,2,3,4,5,6,7,8,9,10};
auto even =…

jcjustesen
- 523
- 4
- 11
27
votes
1 answer
Is const broken with std::views?
void foo(const auto& collection)
{
*collection.begin() = 104;
}
int main()
{
std::vector ints {1, 2, 3, 4, 5};
foo(ints); // Error, as it should be
foo(ints | std::views::all); // Compiles and modifies the vector. Why?
…

Alexey104
- 969
- 1
- 5
- 17
26
votes
1 answer
Why is iterating over std::ranges::views::join so slow
This is a follow-up of this SO Answer. Given a flat input range and three size_t dimensions, the code creates a nested random_access_range of random_access_ranges of random_access_ranges, modelling a three-dimensional array.
Quickbench
Iterating…

joergbrech
- 2,056
- 1
- 5
- 17
22
votes
2 answers
Why does std::views::split() compile but not split with an unnamed string literal as a pattern?
When std::views::split() gets an unnamed string literal as a pattern, it will not split the string but works just fine with an unnamed character literal.
#include
#include
#include
#include
#include…

khaos
- 632
- 3
- 11
21
votes
2 answers
Why can std::vector not accept iota_view iterators of type size_t?
The following code fails to compile when n is size_t but works fine for int and unsigned.
#include
#include
int main() {
size_t n = 1;
auto view = std::ranges::iota_view{n, n};
std::vector test(view.begin(),…

Chris_F
- 4,991
- 5
- 33
- 63
21
votes
3 answers
How does the erase-remove idiom work with ranges/constrained algorithms?
I'm trying to use a c++20 constrained algorithm for the erase-remove idiom:
std::vector v;
v.erase(std::unique(std::begin(v), std::end(v)), std::end(v));
but when I do a simple transformation:
v.erase(std::ranges::unique(v), std::end(v));
I…

cigien
- 57,834
- 11
- 73
- 112
19
votes
1 answer
Do we really need to implicitly convert ranges adaptors to bool?
Since ranges::view_interface has an explicit operator bool() function, this makes most C++20 ranges adaptors have the ability to convert to bool:
https://godbolt.org/z/ccbPrG51c
static_assert(views::iota(0));
static_assert("hello"sv | views::split('…

康桓瑋
- 33,481
- 5
- 40
- 90
19
votes
2 answers
What is std::views::all introduced for in C++20?
#include
#include
int main()
{
auto v = std::vector{1, 2, 3, 4};
v | std::views::drop(2); // ok
std::views::all(v) | std::views::drop(2); // also ok
}
Successfully compiled with g++11 -std=c++20. But I cannot tell any…

xmllmx
- 39,765
- 26
- 162
- 323
19
votes
1 answer
Why do std::ranges algorithms lack parallel overloads (taking an execution policy parameter)?
Many functions in the C++ Algorithms library have overloads taking a parameter of type ExecutionPolicy, e.g. std::copy. However, I noticed that the corresponding functions in the std::ranges namespace do not have these parallel overloads. This is…

SWdV
- 1,715
- 1
- 15
- 36
19
votes
1 answer
What is the difference between std::ranges::begin and std::begin?
What is the difference between std::begin and the new std::ranges::begin? (same for end, size, etc.)
Both seem to work identically:
#include
#include
#include
#include
template
void…

SWdV
- 1,715
- 1
- 15
- 36
17
votes
1 answer
Is the const overload of begin/end of the range adapters underconstrained?
In C++20, some ranges have both const and non-const begin()/end(), while others only have non-const begin()/end().
In order to enable the range adapters that wraps the former to be able to use begin()/end() when it is const qualified, some range…

康桓瑋
- 33,481
- 5
- 40
- 90
16
votes
3 answers
What am I missing in my custom std::ranges iterator?
I'd like to provide a view for a customer data structure, with it's own iterator. I wrote a small program to test it out, shown below. It I uncomment begin(), then it works. But if I use DummyIter, then I get a compile error.
In my full program,…

dromodel
- 9,581
- 12
- 47
- 65
15
votes
1 answer
Why can ranges not be used for the pipes library functionality?
Jonathan Boccara (author of Fluent C++) wrote a library called pipes.
This "piping", the repository's main page says, is not like the use of ranges, even though it looks the same: It's not based on lazy pulling, but rather eager pushing. But it's…

einpoklum
- 118,144
- 57
- 340
- 684
14
votes
1 answer
Detecting compile-time constantness of range size
compiler explorer link
Consider the following:
// Variant 1
template struct require_constexpr;
template
constexpr auto is_constexpr_size(R&& r) {
return requires { typename…

user17732522
- 53,019
- 2
- 56
- 105