A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from
Questions tagged [std-ranges]
379 questions
6
votes
1 answer
requirements for custom container type to use with views
I start to play with std::ranges and want understand how views really work. So I try to write my own container and iterator type and want to use it in a view.
But something seems to be missing but the compiler only tells me that there is no begin()…

Klaus
- 24,205
- 7
- 58
- 113
6
votes
1 answer
Why is const char[] a better match for std::ranges::range than for an explicit, const char* free overload, and how to fix it?
I wanted to write a generic << for any range and I ended up with this:
std::ostream& operator << (std::ostream& out, std::ranges::range auto&& range) {
using namespace std::ranges;
if (empty(range)) {
return out << "[]";
}
…

Fureeish
- 12,533
- 4
- 32
- 62
6
votes
2 answers
Concatenate multiple range adaptors into one single ranges in C++20
Consider the following case:
std::vector v{0, 1, 2, 3, 4, 5};
// 0 1 2 3 4 5
auto rng1 = std::views::all(v);
// 5 4 3 2 1 0
auto rng2 = std::views::reverse(v);
// 4 2 0
auto rng3 = std::views::filter(rng2, [](int x){return x % 2 == 0;});
Is…

康桓瑋
- 33,481
- 5
- 40
- 90
5
votes
0 answers
Why does the compiler not use SIMD in my range-expression?
I have got two implementations of a dot-product:
One hand-coded https://godbolt.org/z/48EEnnY4r
int bla2(const std::vector& a, const std::vector& b){
int res = 0;
for(size_t i=0; i < a.size(); ++i){
res += a[i]*b[i];
…

Stein
- 3,179
- 5
- 27
- 51
5
votes
3 answers
`std::views::split` on MSVC
I want to split a string by a token with std::views::split, and for each retrieved substring invoke the std::from_chars function.
Here is an MRE (https://godbolt.org/z/1K71qo9s4) that compiles successfully on GCC but fails to compile on…

Andrew
- 311
- 1
- 7
5
votes
1 answer
Why is the compiler unable to match the types automatically on `size_t` variables in a ranged base for loop?
I stumbled upon a problem with ambiguous overload for operator<< when using std::views::enumerate with size_t range.
More specifically using this code:
#include
#include
namespace rv = std::ranges::views;
int main()
{
for…

Leo
- 171
- 5
5
votes
3 answers
size() causes error when switching from transform to filter
When
views::filter([](auto n) { return n % 2 == 0; }); is activated, I fail to get [2,4].
views::transform([](auto n) { return 2 * n; }); is activated, I succeed to get [2,4,6,8,10].
#include
#include
#include
int…

The Real Masochist
- 499
- 4
- 10
5
votes
2 answers
can ranges min recalculations be avoided
Goal is to minimize a function over a range of input values. Performance matters.
Unfortunately, the ranges::min() algorithm recomputes the output for the live optimum over and over again.
It seems like the algorithm could cache the output value…

Ludovic Aubert
- 9,534
- 4
- 16
- 28
5
votes
1 answer
Why does C++23 ranges::to not constrain the container type C to be a range?
C++23 introduced the very powerful ranges::to for constructing an object (usually a container) from a range, with the following definition ([range.utility.conv.to]):
template requires (!view)
constexpr C…

康桓瑋
- 33,481
- 5
- 40
- 90
5
votes
0 answers
Why is a const view not a range?
If I create a view, then it's a range. That seems reasonable.
However, if it's const, then it becomes a non-range:
#include
int main()
{
int values[] = { 1, 2, 3 };
auto odd_values = values | std::views::filter([](int i){ return…

Toby Speight
- 27,591
- 48
- 66
- 103
5
votes
1 answer
std::ranges::find_if - no type in std::common_reference
I'm using the SG14 flat_map as a container.
As per a standard map, it takes Key and Value template parameters.
Unlike a standard map, however, it doesn't store std::pair in a binary search tree, but rather stores the keys and values in…

Steve Lorimer
- 27,059
- 17
- 118
- 213
5
votes
1 answer
Is this correct: std::views::reverse on infinite range?
See this example code:
#include
int main() {
for(auto i : std::ranges::iota_view(1) | std::views::reverse)
break;
}
It compiles on gcc (I cannot check on clang/msvc - since they do not support ranges).
Of course -- it runs…

PiotrNycz
- 23,099
- 7
- 66
- 112
5
votes
2 answers
Is std::ranges::size supposed to return an unsigned integer?
Here it is written that std::ranges::size should return an unsigned integer. However, when I use it on an Eigen vector (with Eigen 3.4) the following compiles:
Eigen::VectorXd x;
static_assert(std::same_as

fdev
- 127
- 12
5
votes
1 answer
Range-v3: Why is ranges::to_vector needed here?
I'm trying to compute a reversed views::partial_sum. The code below gives the expected result of a non-reversed partial_'min', but I need to use ranges::to_vector in order to un-views::reverse the final result (since you can't views::reverse a…

Tom Huntington
- 2,260
- 10
- 20
5
votes
3 answers
c++20 ranges library, how to make conditional operator work?
Apologies for the title, if I knew how to better phrase it then google probably already helped me...
I would like to have an object Y, that represents a view of container X, so that when I iterate over Y, it's either forward or backward iteration of…

QnA
- 1,035
- 10
- 25