A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from
Questions tagged [std-ranges]
379 questions
1
vote
1 answer
generic slicing (views) of multidimensional array in C++20 using ranges
In Python, accessing a subset of a multidimensional numpy is normally done using the slicing sintax [bx:ex] for a 1D array, [bx:ex,by:ey] for a 2D array and so on and so forth. It is also possible to write a code which is generic such as
def…

user3646557
- 299
- 2
- 9
1
vote
1 answer
C++20 range from enum
can an enum be used as (or turned into) a C++20 range?
I am thinking about following use case, cartesian_product() is C++23 unfortunatey:
#include
using namespace std::ranges;
enum Fruit{APPLE, STRAWBERRY, COCONUT};
enum…

Ludovic Aubert
- 9,534
- 4
- 16
- 28
1
vote
0 answers
ranges::views:transform failing on clang
The following simple program compiles with g++ (12.1.0) and fails with clang (14.0.6) with a very long and not so descriptive error
#include
#include
int main() {
std::vector v{1, 2, 3};
auto fview =…

Reimundo Heluani
- 918
- 9
- 18
1
vote
1 answer
c++20 why can't I "pipe" into views::reverse like the other views functions?
I am testing some of the fancy new c++ features, one of which is ranges and the associated views. I find it particularly interesting that you can chain what you wish to do with a container.
You can use the binary operator|() to chain things, which…

FalcoGer
- 2,278
- 1
- 12
- 34
1
vote
1 answer
How do I apply modifications to a locally scoped range and return it?
The code below, when compiled under g++-11.3 using --std=c++20 -D_GLIBCXX_DEBUG and executed, produces a bizarre runtime error about iterators. I'm not exactly sure what it means but I suspect it has something to do with the vector range going out…

offbynull
- 381
- 3
- 16
1
vote
1 answer
How can I use ranges max to find the closest point to another given point?
So I've created a structure called point that will consist of two integers. Then, created a function called closest() that's going to take an std::vector containing points, and another point, as a separate argument. It should return a point…

Ulan Duishenaliev
- 47
- 5
1
vote
1 answer
Custom range adaptor only works at the end of composition
I'm building a custom range adaptor, that I called replicate_view, that should return each element of a view N times. Here is an example:
for (auto i : std::views::iota(1, 5) |
views::replicate(2))
std::cout << i << '\n';
This…

Marius Bancila
- 16,053
- 9
- 49
- 91
1
vote
1 answer
Can std::ranges be used with std::list
I note that many algorithms can be used with ranges allowing the use of members of custom types, rather than needing lambda functions. So, am curious whether std::ranges can be used efficiently with std::list<>. I am cognisant that std::list<> is…

willofirony
- 63
- 6
1
vote
2 answers
Can ranges::equal be used as a predicate for a pair of transform_view ranges?
Example Program and Compiler Errors
The following code produces two compiler errors (MSVC++ 2022 compiling with /std:c++latest because isn't yet enabled for /std:c++20 at time of writing this question):
error C2672: 'operator…

Ben Cottrell
- 5,741
- 1
- 27
- 34
1
vote
1 answer
Why is std::ranges::views::take using templated type for difference?
Signature of take is
template< ranges::viewable_range R, class DifferenceType >
requires /* ... */
constexpr ranges::view auto take( R&& r, DifferenceType&& count );
It is a minor thing but I wonder why DifferenceType is not some ssize…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
1
vote
0 answers
ranges fill does not compile
int n=10;
vector translated_ancestors(n) ;
std::fill(begin(translated_ancestors), end(translated_ancestors), false);
Compiles with no errors.
But if I change to:
ranges::fill(translated_ancestors, false) ;
It returns following error…

Ludovic Aubert
- 9,534
- 4
- 16
- 28
1
vote
1 answer
ranges::sort does not compile
In MyRect.h:
struct MyRect
{
MyRect(std::initializer_list i);
MyRect();
int16_t m_left=0, m_right=0, m_top=0, m_bottom=0 ;
int16_t no_sequence=0 ;
int16_t i=-1 ;
bool selected=false ;
} ;
bool operator==(const MyRect&…

Ludovic Aubert
- 9,534
- 4
- 16
- 28
1
vote
1 answer
std-ranges for string splitting and permutations
I'm trying to build a view that takes a vector of strings, splits those strings into pieces on char ; and returns permutations for the resulting tokens on each line.
int main()
{
std::vector lines;
auto strsplit_view =…

Jonas Hjulstad
- 395
- 1
- 8
1
vote
1 answer
Valgrind thinks my std::ranges of raw pointers are leaking, even after they've been copied to unique pointers
I'm trying to load all true-type fonts in a directory using C++20 ranges and functional-style programming. However, since fonts are a resource, I'm allocating memory within the ranges interface. I think this is why valgrind thinks I have a leak. I…

Josie Thompson
- 5,608
- 1
- 13
- 24
1
vote
1 answer
weakly_incrementable constraint while using iota_view
To quickly create a for loop similar to python's for i in range(100), I could do:
for (auto const i : std::views::iota(0, 100))
{ /* ... */ }
However, CLion is warning me to add a std::weakly_incrementable constraint to my auto:
for…

Ranoiaetep
- 5,872
- 1
- 14
- 39