简体   繁体   中英

Can you zip with the new ranges library?

See: http://eel.is/c++draft/#ranges

Given two C++2a ranges (as in objects that conform to the ranges concept of the ranges library) a and b, of equal length, is there a way to zip them together such that:

for (const auto& [a,b] : zip(a,b))

does what you expect? That is, it returns a range that has something destructurable binding pairs:

(a.begin(), b.begin())
(a.begin()+1, b.begin()+1)
(a.begin()+2, b.begin()+2)
...
(a.end()-1, b.end()-1)

As you can see, there is no zip_view currently in C++20 (as of this writing).

It was being proposed in P1035R4 (along with a handful of other adapters), the previous version of which was favorable received in San Diego and seemed like it has a very reasonable chance of landing in C++20. There are open questions regarding proxy references, but I don't think that's specific to zip .


Those questions regarding proxy references ended up causing zip to get dropped from P1035 and it was not adopted for C++20. Instead, zip is being proposed for C++23 as part of P2321 (which additionally includes a description of the kinds of proxy reference changes I mentioned).

ranges v3 niebler has already made public a library with a lazy zip and some more general forms also zip_view and others. Implemented as a header only library so you can read the code. With some nice examples of uses. In c++ zip will probably return a tuple when asked. and indexing into that tuple will give you the values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM