A library for C++20 that introduced std::ranges namespace, which consists of rangified algorithms from
Questions tagged [std-ranges]
379 questions
3
votes
1 answer
Why do I get an error: cannot decompose inaccessible member when compiling this code using std::views::chunk?
This code compiles and works correctly:
#include
#include
int main() {
const auto r = std::views::iota('a', static_cast('g' + 1));
for(const auto& [start, end] : r | std::views::chunk(3u)) {
for(auto it =…

Alberto Santini
- 6,425
- 1
- 26
- 37
3
votes
1 answer
views::iota vs ranges::iota_view - "expression equivalent" so why both exist?
#include
#include
int main()
{
for (int num: std::ranges::views::iota(0,5)) {
std::cout << num << 'n';
}
for (int i : std::ranges::iota_view{55, 65}) {
std::cout << i << '\n';
}
}
CPP…

Bob
- 4,576
- 7
- 39
- 107
3
votes
1 answer
Why doesn't this code using std::ranges::find on an std::views::zip compile?
The following C++23 code does not compile with gcc:
#include
#include
#include
int main() {
std::vector v1{1,2,3,4};
std::vector v2{2,4,6,8};
auto z = std::views::zip(v1, v2);
…

Alberto Santini
- 6,425
- 1
- 26
- 37
3
votes
1 answer
How to design the interface for a function that can be called on both containers and views, while considering thread safety in C++ 20?
I am designing a function in C++20 that needs to be callable on both containers and views. However, I want to ensure thread safety and avoid concurrency issues when the function is called in parallel on the same view / container. The function does…

Abdullah Mohammed
- 39
- 2
3
votes
1 answer
Can I use std::all_of (or better, std::ranges::all_of) over a heterogeneous collection?
I have some code that depends upon various std::shared_ptr values having been initialized. In the case where one or more of these values have not been initialized, I'd like to just return from the function. What I want is something like this:
class…

jwm
- 1,504
- 1
- 14
- 29
3
votes
1 answer
What is a concrete use case of std::ranges::partial_sort_copy with *different* projections?
I root caused a problem in my code to mismatched projections in std::ranges::partial_sort_copy. After reading cppreference, I couldn't think of a legitimate situation where proj1 & proj2 needed to be different.
What's the motivation for this…

MarkB
- 672
- 2
- 9
3
votes
2 answers
Creating a "double zip" view in C++23
I am trying to learn C++20/C++23 features and am stuck on the following problem.
Suppose there are two vectors of arrays of the same size:
std::vector> v1 = { {1, 2}, {3, 4}, {5, 6} };
std::vector> v2 = { {7,…

lc_vorenus
- 31
- 3
3
votes
1 answer
Why does the random access property of iota_view depend on the element type? And how to write a Foo such that iota(Foo{1}, Foo{10}) is random-access?
The following code fails to compile if you uncomment the commented line. (Compiler Explorer)
Why is that?
I imagine the reason is that the iota_view of Foos doesn't know how to advance a Foo. But how do I make it know?
#include
#include…

Enlico
- 23,259
- 6
- 48
- 102
3
votes
2 answers
range parameter for any iterable input with properly convertible elements
I am currently trying to get used to C++20 especially concepts and ranges in this case. Hopefully the title fits my problem I am not really sure yet what I ran into.
I want to create a set method for my class AttribueMap to pass any iterable input…

Tobxon
- 35
- 4
3
votes
1 answer
Deleting map elements in a range-based loop
I would like to drop a number of elements from a map based on some condition:
#include
#include
#include
int main() {
std::unordered_map numbers = {{1,2}, {2,1}, {3,2}, {4,5}};
auto even =…

Stein
- 3,179
- 5
- 27
- 51
3
votes
1 answer
std::views::istream with std::views::take
I compiled the following code with g++ 12.2.1:
#include
#include
#include
#include
#include
int main()
{
std::vector vi;
std::ranges::copy(std::views::istream(std::cin) |…

Blackteahamburger
- 595
- 1
- 18
3
votes
1 answer
How can I design a standalone C++17 std::views::join alternative?
For a C++17 restricted project I would like to have a standalone implementation of C++20 std::views::join(). I am aware of the existence of range-v3 but unfortunately the person in charge is unwilling to include further 3rd party libraries.
My aim…

joscao
- 35
- 7
3
votes
1 answer
Possible ways to make this `cartesian_product_with_filter` function variadic?
I am trying to implement a function that calculates the Cartesian product of ranges. I know there's views::zip and views::cartesian_product available, and am just deepening my understanding of the use of the range library by writing this function…

FeignClaims
- 193
- 8
3
votes
2 answers
Caching inside view object leads to UB?
#include
#include
#include
int main()
{
std::vector ints {1, 2, 3, 4, 5};
auto isEven = [] (const auto& element)
{
return element % 2 == 0;
};
auto even = ints |…

Alexey104
- 969
- 1
- 5
- 17
3
votes
2 answers
How to use iota_view for custom IntLike types?
I would like to use std::ranges::iota_view for a custom data type that is "int" like in the same way I would use std::ranges::iota_view for an int. I couldn't figure out how to adjust the code based on gcc's error messages, which are posted…

MarkB
- 672
- 2
- 9