588 questions
1
vote
2
answers
61
views
Invalid use of std::ranges / std::filter
Below is a minimal example of an attempt to use the std::ranges library to look at a map<int,...> and return only the first elements such that the int is contiguous starting at one. However, the ...
5
votes
1
answer
128
views
How to copy values from array to array of structures?
In the code below I could use convenient syntax of std::ranges::transform to copy values from array of structures (AoS) to structure of arrays (SoA) (in this example, just one vector). Is there a way ...
3
votes
1
answer
177
views
In what situations does `ranges::for_each` work but `for (auto&& elt : rg)` fail?
Given the following C++23 code (Godbolt):
template<class R, class T>
concept container_compatible_range =
std::ranges::input_range<R> && std::convertible_to<std::ranges::...
2
votes
1
answer
102
views
Why is the type of the return value of ranges::count_if dependent on the input range but only in Visual Studio?
The following succeeds with GCC and Clang but causes an error when compiled with Visual Studio: (compiler explorer link)
#include <functional>
#include <algorithm>
#include <ranges>
#...
-1
votes
0
answers
93
views
I meet a bug about std::views::adjacent_transform,so i want figure it out [closed]
int main() {
vector<int> nums{-15,19};
auto d = nums
|std::views::chunk_by(std::ranges::less{})
|std::views::transform(std::ranges::...
1
vote
1
answer
75
views
Not every view is a borrowed_range, but is every borrowed_range a view?
view does not imply borrowed_range
For example transform_view is not a borrowed_range, see:
https://stackoverflow.com/a/76789448/7806123
Does borrowed_range imply view?
As it is cheap to copy, does it ...
1
vote
2
answers
48
views
How should a coroutine accept a const range?
I want to implement a coroutine that accepts a range and keeps it intact (modules const reference).
Let coroutine_foo be a coroutine: How would you implement its signature?
Since it is expensive to ...
4
votes
1
answer
102
views
C++20 ranges - How to store a composed ranges view in class member
How can I store a C++20 ranges composed view as a class member?
I tried with the following code which works with in general, but if I put the struct into a header, GCC warns me that splitter::view has ...
1
vote
1
answer
109
views
Make similar structs be convertible in C++20 or C++23
I'm trying to get a constructor that accepts ranges, either vectors or arrays of a user defined struct.
I'm using std::convertible_to<>
The compiler error message is where I call the constructor....
0
votes
2
answers
116
views
Single-element range
Given a function that accepts a range, for example:
void f(const std::ranges::range auto& r);
Sometimes I have only one element that I want to pass to the function.
There are multiple ways of ...
2
votes
1
answer
168
views
What is the ideal way to customize the separators and brackets for formatting ranges?
In C++23, we can use std::print directly to print ranges thanks to P2286:
std::vector v = {10, 2, 42, 15};
std::println("{::02X}", v); /* 02X is the format spec for elements of range */
...
3
votes
1
answer
123
views
Is there a wrapper for function call for std:::generator?
Is there an existing wrapper to call a function for std::generator like in the code below?
Although mine works just fine, I don't won't to reinvent the wheel, since code would be more complex for ...
-1
votes
1
answer
77
views
Can std::ranges::enumerate enumerate any possible valid C++ array?
Once I asked whether std::ranges::views::enumerate uses the wrong type (long) for indexing on GCC, but apparently that's not the case, because
std::views::enumerate is specified to use ...
0
votes
0
answers
45
views
How to filter vector before applying std::ranges::minmax_element? [duplicate]
When I try to filter std::vector elements before applying std::ranges::minmax_element by some reason I get the std::ranges::dangling which is hard to handle and can’t be used to get the access to the ...
6
votes
2
answers
155
views
std::ranges::sort not working with non-default operator<=>?
I have a case when struct type needs non-default operator<=> - just not all fields are important when defining order of objects.
struct A
{
int a;
int b; // not important when ordering
...
1
vote
1
answer
73
views
How do I output to a file via Ranges from C++
I have a simplified analogue of netgen in C++. In one pipeline, I need to output to a file the coordinates of those nodes (as a container std::array of three elements) that fall inside a sphere of a ...
3
votes
0
answers
87
views
What is the purpose of putting function objects in a copyable-box or movable-box? [duplicate]
In the C++ ranges library function objects, such as the ones stored in transform_view and filter_view, are put in an exposition-only type called a copyable-box (until C++23) or movable-box (since C++...
3
votes
1
answer
121
views
C++ vector constructed directly as range argument not working as expected
This is different when I construct a vector directly as an argument vs. using one defined as a variable, can someone explain why?
#include <cstdlib>
#include <iostream>
#include <...
2
votes
0
answers
99
views
Dynamically allocated multidimensional arrays in C++
With the recent developments of C++ do we have some common solution for multidimensional arrays which sizes known at run time only and which don’t change during execution?
I found only the Elegantly ...
1
vote
1
answer
96
views
How can you use std::views::zip with std::ranges::for_each?
With C++ foreach loops we can write:
void process(int i);
...
for(auto i : items)
process(i);
With ranges in C++20, this can be written as:
std::ranges::for_each(items, process);
With std::views:...
2
votes
4
answers
118
views
How to return a range::view in a std::expected?
Is there a way to return a range::view in a std::expected construct? For example, in the pseudo code below, what can I write instead of the rangeType?
std::expected<rangeType, error> get_range()
...
0
votes
1
answer
65
views
How to keep a passed in temporary vector from getting taking by std::ranges
I have this function:
template<std::ranges::viewable_range T>
auto foo(T&& ts) -> size_t
{
auto moves_ts { std::forward<T>(ts) | std::views::all };
return ts.size(); // ...
1
vote
2
answers
94
views
Preserving std::forward_range during chunk transformation?
encode below is a dumbed down version of a transformation I am working on (run it on godbolt):
#include <array>
#include <iostream>
#include <ranges>
int main() {
static ...
1
vote
0
answers
98
views
How to write custom forward range iterator working with C++20 ranges
My original question
How can I write a C++ class that works with C++20 std::ranges library?
Origin of my problem
I tried to write a facade class with iterators in C++. But my compiler (clang++18) told ...
3
votes
0
answers
91
views
How to properly read data from a stream with C++20 ranges library?
With C++20, new ranges library was introduced to present an alternative way to deal with sorts of sequences (rather than simple for/while cycles and <algorithm> approach with iterators).
It is ...
-1
votes
2
answers
81
views
Using range-based for loops and range adaptors with non-copyable input iterators
While the legacy C++17 named requirement InputIterator requires the iterator to be copyable, the new C++20 concept std::input_iterator does not.
But I tried to use std::views::drop(1) on a custom ...
1
vote
1
answer
136
views
std::views::chunk, std::views::transform, inputs ranges, and ill-formedness
Consider program 1:
#include <algorithm>
#include <iostream>
#include <ranges>
#include <sstream>
int main() {
std::istringstream iss{"\x{01}\x{02}\x{03}\x{04}"};...
0
votes
0
answers
52
views
Using ranges::transform to write to uninitialized memory for trivial types
I have a question regarding using std::ranges::transform to write data into uninitialized memory. Consider full example: https://godbolt.org/z/WbzzY9qY5
TLDR: Is the following code valid?
auto ...
3
votes
2
answers
264
views
Am I misunderstanding how `std::views::cartesian_product` is supposed to work?
Edit: see also a connected question.
I have tested the following program on godbolt:
#include <algorithm>
#include <iostream>
#include <ranges>
#include <sstream>
int main() {...
3
votes
1
answer
129
views
About preventing errors in use of Range adaptor objects
The code below compiles, but is meaningless and clearly indicates that the programmer made a typo:
std::vector a{ 1, 2, 3};
auto x = std::views::take(a);
auto y = std::views::drop(a);
...
1
vote
0
answers
62
views
How can I create a custom C++20 view (like std::ranges::views::filter) [closed]
I am looking for a clear resource or tutorial on how to create a custom view in C++20. (like std::ranges::views::filter).
I have checked Microsoft and cppreference documentation but couldn't find ...
6
votes
2
answers
203
views
How do I reuse a C++23 `std::generator` instance as a range in multiple range expressions?
I'm trying to learn C++ ranges. As far as I can see, the simplest (and the only, short of implementing a custom range view class) way to create a range view object that generates a custom sequence is ...
0
votes
1
answer
97
views
Why is it done in std::ranges that I can't split the range that I got from join with transform
Why is it done in std::ranges that I can't split the range that I got from join with transform
Why doesn't this code compile and how can I fix it while still using the declarative approach with std::...
2
votes
2
answers
246
views
C++ 20: trying to return std::ranges::view from a function
I am tasked with investigating performance improvement of some legacy C++ code, by leveraging CPP20. One potential area of improvement I see is this pattern in the hot path.
std::vector<...
4
votes
1
answer
146
views
Is it legal to dereference end() on std::views::iota?
I found some code that dereferences end() from an iota_view.
I asked the author about it and they pointed out that end() of an iota_view where Bound and W are the same type is iterator{bound} and ...
2
votes
2
answers
164
views
Unexpected runtime benchmarks with C++20 ranges
Here 'myData' is a vector of integer. I filter only even numbers from this and transform these even numbers into string. These are stored in a vector and returned.
This function is called and we call ...
4
votes
1
answer
74
views
std::views::split copies the pattern instead of making a reference
According to cppreference, std::views::split call signature (https://en.cppreference.com/w/cpp/ranges/split_view ):
template< ranges::viewable_range R, class Pattern >
requires /* see below */
...
1
vote
1
answer
99
views
How to implement generator that can be used with ranges views
I'm using C++20, and I'm trying to implement a custom generator. I'm reusing an implementation of the Generator from here: https://en.cppreference.com/w/cpp/coroutine/coroutine_handle
This example ...
1
vote
0
answers
52
views
Clarification on How Rvalue Ranges Become Owning Views in C++ [duplicate]
I need some clarification:
The reason functions 1 and 2 are considered OK is because when the initial range is an rvalue (either a temporary object or an object marked with std::move()), the ...
4
votes
1
answer
130
views
Questions about ranges::distance implementation
I'm reading ranges::distance source and don't get the idea that:
Why is the struct __distance_fn final?
What is the purpose of void operator&() const = delete (source)
I know that a final class ...
3
votes
1
answer
100
views
How to sort descending with cpp 20 ranges projections
Say I have a Person class like this.
class Person
{
friend std::ostream &operator<<(std::ostream &out, const Person &operand);
public:
Person(std::string name, std::string ...
2
votes
0
answers
33
views
Parallel for_each without container with non-constexpr std::views::iota [duplicate]
I'm looking for a replacement for
std::vector<double> data(n, 0);
for(int i = 0; i < n; i++){
data[i] = func(i);
}
which can be called with std::transform with a parallel execution policy ...
3
votes
2
answers
97
views
Using zip_view piped into a filter_view
I am getting compilation erorr while trying to zip two vectors and apply and filter as below.
#include <range/v3/all.hpp>
int main()
{
std::vector v1 = {0, 1, 2, 3, 4};
std::vector v2 = ...
1
vote
3
answers
131
views
Implementing take_while_inclusive (or std::delimit): views::take_while but including element missing predicate
I was working on a simple parser using std::ranges. I was trying to parse ints in a string until all were converted or one failed by doing something like:
try_parse_ints(str) | take_while(is valid int)...
2
votes
0
answers
119
views
Why do the std::ranges algorithms have overloads for taking iterator-sentinel pairs?
I'm currently designing an API for a new library, trying to model it after the std::ranges algorithms. I notice that in addition to taking ranges as parameters, these functions have overloads to allow ...
3
votes
0
answers
53
views
stack-use-after-return with projection to a member inside a ranges pipeline [duplicate]
I have a struct containing a member int:
struct S {
int i;
};
and a function that returns an instance of this struct, constructed from an int:
S f(int) {
return {42};
}
and I'm using this ...
3
votes
3
answers
200
views
Is there a way to get projection to zipped vectors std::tuple without lambda?
As shown in the code below I can sort zipped vectors with std::ranges::sort, using lambda to specify that sorting should be done by the first vector. Is there a way to use projection, specifying the ...
0
votes
1
answer
94
views
Underlying Reason for Why ranges::filter_view is Modifiable but ranges::transform_view is not
I've been exploring C++20 ranges and views and used the cppinsights.io website to dig into the details. I noticed something interesting about how filter_view and transform_view behave differently when ...
4
votes
1
answer
141
views
Why cannot I generate pairs of numbers from a std::views::iota in c++ 20
I wrote the following function to generate pairs from input ranges.
template <typename Range>
requires std::ranges::borrowed_range<Range>
auto pair(Range && r)
{
// Creating a ...
4
votes
3
answers
149
views
Why the type of the iterator to `std::views::transform` does not seem to be a deterministic type?
I want to get the type of the iterator to the const-casted contents of the vector. I thought I can use decltype for it.
Apparently it is not that simple.
#include <ranges>
#include <vector>...