Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a ranges::to_container(T container) which accepts a reusable container #6548

Closed
dongweigogo opened this issue Sep 1, 2023 · 2 comments
Closed

Comments

@dongweigogo
Copy link

According to C++23 draft, there will be a ranges::to() to be implemented.

Constructing containers and strings from ranges with ranges::to(), tagged constructors, insert and assign member functions.
P1206R7

I tested ranges::to() in develop version of clang because current gcc does not support. It works but I found that it has a rather poor performance.

vector<int> v auto vec = views::iota(-10, 10) | views::filter([](int i) {return i>0;}) | ranges::to<vector<int>>();

I think that the main reason is that a new container has to be created every time the view runs. Thus it is reasonable if we could supply a variant ranges::to() which accept a reusable container as a parameter. like ranges::to_container(T container).

There are at least two benefits based on the above thought:

  1. The reusable container can avoid unnecessary reallocating ;
  2. We can collect different views into a same container;

Since ranges::to() is somehow similar to collect() in rust lang, I found a couterpart of ranges::to_container(T container) there:

a.iter().collect_into(&mut vec);

The collect_into() is what I would like to propose.

@jwakely
Copy link
Member

jwakely commented Sep 1, 2023

This repository is just for the text of the standard, not for proposed changes or additions to the standard. Please see https://isocpp.org/std/submit-a-proposal instead.

@jwakely jwakely closed this as not planned Won't fix, can't repro, duplicate, stale Sep 1, 2023
@frederick-vs-ja
Copy link
Contributor

Such a stuff is almost same as c.assign_range(expr) except that it returns a reference to c.

If you don't need to immediately refer to the container in the same expression, you can just use assign_range.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants