C++ Logo

std-proposals

Advanced search

[std-proposals] Fwd: ranges::collect

From: Corentin Adam <corentin.adam27_at_[hidden]>
Date: Wed, 11 Sep 2024 18:55:56 +0200
Hi everyone,

I've recently been working on a cpp implementation of the rust collect
function: :
https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.collect.

This function has two main uses in rust:
 * construct containers from ranges (exactly the work achieved by
std::ranges::to)
 * collect ranges of expected/optional values into expected/optional range
of value. Thus, if all values in the range are correct (in other words
has_value() == true), the return value is true and contains the range of
underlying values. Otherwise, the return value contains the first error
encountered in the range.

Rust example of this second usage :

[image: image.png]

This second usage is really useful, is commonly used in rust and I think it
could be a great addition in std::ranges.
Here is a simple example of the same behavior in cpp with my function :
https://godbolt.org/z/6K3sG3s7M

As I said, I implemented it recently because I needed it for a personal
project, and you can find more explanation about it in my github repo here:
https://github.com/jileventreur/collect
It's a work in progress and I think there's some work on the implementation
details that would need to be done (and if you want I could expand on that)
but I think it could be a starting point for standardization.

Thank in advance

Corentin Adam

Received on 2024-09-11 16:56:11