641 questions
4
votes
2
answers
146
views
Can I compare two view sentinels?
I am implementing type-erased iterators for code that uses std::views and have found a problem while trying to compare values that wrap sentinels. Basically, it seems that for some composition of ...
20
votes
4
answers
2k
views
How can I combine corresponding items from several vectors into a single vector of structs?
I have three std::vectors,
std::vector<std::string> vec1{ "one", "two", "three" };
std::vector<std::string> vec2{ "1", "2", "3" }...
4
votes
1
answer
128
views
Is it valid to join a transform view whose elements are temporary containers?
Consider this C++23 example:
#include <ranges>
#include <print>
#include <vector>
namespace r = std::ranges;
namespace rv = std::ranges::views;
std::vector<int> ...
-3
votes
1
answer
204
views
Splitting file content using C++20 ranges [closed]
I'm trying to iterate through a text file's lines using ranges:
auto iter_lines(std::ifstream& file)
{
auto lines = std::ranges::istream_view<char>(file) |
std::views::lazy_split(...
2
votes
2
answers
140
views
Structured binding for std::views::enumerate of std::map [duplicate]
I'm trying to do something like this:
const std::map<char, int> histogram{
{'A', 2},
{'D', 1},
{'M', 1},
};
for (const auto& [index, key, value] : std::views::enumerate(...
5
votes
1
answer
173
views
Is it okay to pass projection as predicate argument to std::ranges::any_of/all_of/none_of?
This works, but is it legal/safe?
(i.e.: Is it okay to pass projection as predicate argument to std::ranges::any_of/all_of/none_of?)
#include <ranges>
#include <iostream>
#include <...
1
vote
1
answer
134
views
Can I use std::views::transform to create view of abstract base classes?
The following code does not compile.
#include <iostream>
#include <memory>
#include <ranges>
#include <vector>
class Base
{
public:
virtual ~Base() = default;
...
1
vote
0
answers
57
views
What is the category of transform view based on vector? [duplicate]
What is the category of std::views::transform based on a std::vector? What is the category of the view's iterator in the following code?
#include <iterator>
#include <memory>
#include <...
5
votes
1
answer
166
views
Are constraints in C++ std::ranges::ref_view constructor redundant, and is the forwarding reference necessary?
The libstdc++ implementation of std::ranges::ref_view, following the C++ standard, includes the following code:
template<range _Range> requires is_object_v<_Range>
class ref_view : ...
6
votes
1
answer
279
views
Do C++20 `std::views` store copies or references to their input ranges?
C++20 adds numerous std::views::... range adaptors.
When applied to a range, do they store a copy of the range, or a reference to it?
In other words:
std::vector<A> v;
for (const auto &elem :...
1
vote
1
answer
125
views
std::ranges::transform(R&& r, O result, F op, Proj proj = {}) can I use parameter op for projection?
I want to transform std::vector<A> to std::vector<B> using
std::ranges::transform(R&& r, O result, F op, Proj proj = {}),
but I have no post-transform functor op.
It seems to work ...
0
votes
0
answers
84
views
How can I obtain a copyable value type from a transform_view<enumerate_view<...>>?
tl;dr
Given this code:
#include <functional>
#include <optional>
#include <range/v3/range/conversion.hpp>
#include <range/v3/range/primitives.hpp>
#include <range/v3/view/...
2
votes
1
answer
273
views
Why can't C++23's enumerate_view (unlike Range-v3's namesake) be piped into Range-v3's to_vector?
Here's the minimal example:
#include <vector>
#include <ranges>
#include <range/v3/range/conversion.hpp>
#include <range/v3/view/enumerate.hpp>
int main()
{
std::vector&...
4
votes
2
answers
183
views
Why is `iterator_category` deleted in `std::views::concat::iterator` if it's a pure input iterator?
In the C++26-adopted proposal p2542, i.e. std::views::concat, there is a confusing statement:
The member typedef-name iterator_category is defined if and only if all-forward<Const, Views...> is ...
-1
votes
1
answer
164
views
Why is `std::views::as_rvalue` not so cheap as `std::move_iterator`? [closed]
Consider the following code snippet (full code: https://godbolt.org/z/PMM4z9KvM):
int main() {
{
std::cout << "begin std::views::as_rvalue\n";
auto iss = std::...
8
votes
2
answers
257
views
Why do `std::views::adjacent` iterators increment all underlying iterators instead of using a more performant shift?
In C++23, std::views::adjacent<N> provides a sliding window of N elements over a range. Its iterator typically stores N iterators to the underlying range. When implementing this iterator's ...
3
votes
2
answers
151
views
How to reduce verbosity when splitting `std::string_view` by `std::views::split`?
Consider the following snippet:
// included, for exposition only
SomeData buzz(std::string_view);
constexpr auto foo(std::string_view v) {
using namespace std::string_view_literals;
return v ...
4
votes
1
answer
131
views
Binding std::generator<T> with pybind11?
I am trying to bind a std::generator<T> to a Python generator through pybind11, I am using the following currently:
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include ...
4
votes
2
answers
192
views
lifetime of temporary ranges in c++
In the code below std::ranges::minmax_element return iterators on a temporary view.
In the next line the iterator is dereferenced.
Copilot claims that the code is safe because the lifetime of the ...
2
votes
1
answer
186
views
Why can't I flatten a range of std::meta::info with std::views::join?
I have the following code, and I can not flatten the nested range using std::views::join
template<typename T>
consteval auto get_names() {
using get_fn = decltype(
[] {
auto ...
3
votes
1
answer
178
views
How do I put a `std::expected` into a range?
The following code compiles:
#if 0 /*
g++ -std=c++23 $0 -o exe \
-Wall -Werror -Wextra -Wsign-conversion -pedantic-errors \
&& ./exe $@
RET=$?
rm -f exe
exit $RET
*/
#endif
#include <...
0
votes
0
answers
97
views
Conversion between view types in std::ranges [duplicate]
I'm trying to provide different searching ranges for my searching function :
//In a function that returns a view of std::vector
...
if(mode == 0) // filter some elements
return my_vector | std::...
1
vote
2
answers
102
views
error compiling ranges::set_intersection with projection
The code:
#include <algorithm>
#include <ranges>
#include <vector>
#include <tuple>
#include <generator>
#include <cstdio>
#include <iostream>
using ...
1
vote
1
answer
137
views
Is it possible to use std::ranges::transform_view with incomplete types?
I'm writing a C++23 program and trying to create a recursive data structure with help from std::ranges::transform_view. However, it seems like the requires clauses on the std::ranges::transform_view ...
2
votes
2
answers
118
views
can a range of generators be joined
Is it possible to have a range of generators and if yes, is it possible to join them ?
#include <algorithm>
#include <ranges>
#include <vector>
#include <cstdio>
#include <...
3
votes
1
answer
117
views
Why does my transformed range not have a valid iterator?
I want a view of the characters from an input stream:
auto input = std::stringstream{"abcd"};
using Iter = std::istreambuf_iterator<char>;
auto s = std::ranges::subrange{...
4
votes
1
answer
199
views
How to make join view in C++ preserving random access?
The code below breaks at last line as joined view v isn't a random access range.
vector<string> v0 = {"word","good","best","good"};
auto it = v0.begin() + ...
5
votes
1
answer
285
views
Replacement for std::views::zip/keys in C++20
I need to port some code from C++ 23 which uses std::views::zip/keys to C++ 20 (porting to CUDA) (which doesn’t have std::views::zip) having only one specific use case of this code.
I have data in one ...
8
votes
2
answers
201
views
What is the category of iota_view iterator?
I was experimenting with std::iterator_traits and std::views::iota. And suddenly I found out that std::iterator_traits returns unexpected type for an iterator category.
libc++:
#include <ranges>
...
9
votes
1
answer
236
views
Why chained range views sometimes increase in size?
I have been playing around with std::ranges::views and have noticed that every chained view gets bigger. For view iterators this pattern is simple, each is always 8 byte bigger than the one before. ...
2
votes
0
answers
153
views
C++ ranges: Change a parameter within a view
given the following MWE
#include <iostream>
#include <vector>
#include <ranges>
#include <algorithm>
#include <numeric>
namespace{
auto doSomething(const bool b)
{
...
1
vote
2
answers
196
views
How to obtain a range to loop over containers in variants
I have the need to map some elements to certain ids, elementId's to starting node ids in a graph. I need to store just ids of the elements so generally we could work with just sets or vectors of ...
21
votes
2
answers
1k
views
Why does std::views::take_while() do so many function invocations? (even with `cache_latest`)
I decided to use std::views stuff because of their bound-safety and readability. It looks like std::views::take_while invokes too many function calls and recomputes the same thing over and over again. ...
2
votes
1
answer
399
views
Using std::ranges::to with std::array
I would like to have this code with std::vector replaced with std::array, but I presume it can not be done since std::array is not a container std::ranges::to understands.
constexpr auto dice = std::...
0
votes
1
answer
131
views
Can I compose `take_while` and `filter` and mutate underlying sequence?
This is a follow-up to Debug assertion while iterating over composed views.
I replaced problematic transform_view with a loop and the debug assertion was gone. Then I added a filter and the problem is ...
6
votes
1
answer
124
views
Debug assertion while iterating over composed views
I know that modifying the target of filter_view::iterator is allowed, but if the resulting value does no longer satisfy the filter predicate this results in undefined behaviour.
What about the ...
5
votes
0
answers
45
views
Passing R-values to ranges with range-v3 and with std::ranges [duplicate]
The range-v3 library complains if I try to feed an r-value to the pipe operator:
#include <iostream>
#include <range/v3/view/transform.hpp>
#include <vector>
std::vector<int> ...
2
votes
2
answers
108
views
Apply std::set_difference to the keys of two std::maps which each have differing value-types
I have two maps. One (A) is maps unique identifiers to allocated resources. The other (B) maps unique identifiers to resource prototypes. I would like to take the two maps and find the items that are ...
4
votes
3
answers
164
views
std::views like operation on parameters pack
I need sometimes to filter some types from a given parameter pack and get the result as a std::tuple. For instance, it can be a filter based on the index of the types in the pack.
As an example, here ...
1
vote
2
answers
143
views
Solution or alternative for recursive view
We want to implement a tree of items:
+----------------------+
| Item (interface) |
+----------------------+
| + getName(): string |-----------+
+---------------...
3
votes
1
answer
98
views
Issue with a custom range when using both std::views::join and std::views::enumerate
I have a simple SequencesRange class that can iterate random genomic sequences of fixed size.
When I try to join the characters of all sequences with std::views::join, it works well, but as soon I try ...
4
votes
1
answer
130
views
How can I elegantly fill/initialize two (or more) ranges from a range containing pairs/tuples of elements?
For zipping ranges together, we have std::views::zip and friends.
Is there a way to "unzip" a range, i.e. write the following "index filter" function as a range expression?
auto ...
2
votes
0
answers
132
views
How should I declare an interface function that gets a range of ints?
I want to pass a std::ranges::view to a function which is an implementation of an interface, without creating a new vector and allocating more memory.
virtual void doSomethingWithInts(std::ranges::...
4
votes
1
answer
238
views
How do you use std::views::zip (and friends) with the pipe syntax?
C++23 gives us the long-missing std::views::zip (and friendly helpers like std::views::zip_transform) which gives a handy way to take values from multiple ranges and do something with those sets of ...
14
votes
1
answer
414
views
Counting the number of present values in array of optionals with std::ranges
My colleague ports a C++ program with ranges on macOS, and observes an unexpected compilation error.
After maximum simplification, the example program looks like this:
#include <optional>
#...
0
votes
2
answers
165
views
Creating a std::range on std::cin and view composition
I am going through 'Functional Programming in C++' (yes, it is getting a bit long in the tooth, but it is what I have right now), and I am trying to convert some of the examples in it to C++20 (g++ 13....
0
votes
1
answer
97
views
Why can't a view be assigned with the same type of the view?
Why can't I (re)assign the value for a view with the same type of the view?
Demo: https://godbolt.org/z/aqdh7ohva
#include <iostream>
#include <ranges>
#include <vector>
int main()
{...
2
votes
2
answers
100
views
How to update the range with a filtered one?
I have a filter which should be conditionally applied to some range and the condition is known at execution time only.
I can do this easy, putting the condition in the filter, but this leads to high ...
1
vote
2
answers
104
views
Why is my data garbled using ranges::to<std::vector>?
I am working with the ranges library and pipelines to read CSV files. My code works okay except when I add ranges::to<std::vector> at the end of the pipeline. With the 'to' added, the last line ...
4
votes
1
answer
156
views
Why is this std::view getting evaluated twice?
I have the following test program:
#include <vector>
#include <print>
#include <ranges>
int main() {
const std::vector<int> input = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
...