This is an archive of the discontinued LLVM Phabricator instance.

[libc++][ranges] Implement `ranges::generate{,_n}`.
ClosedPublic

Authored by var-const on Jul 26 2022, 12:04 AM.

Details

Reviewers
huixie90
ldionne
Group Reviewers
Restricted Project
Commits
rGead7302bbb14: [libc++][ranges] Implement `ranges::generate{,_n}`.

Diff Detail

Event Timeline

var-const created this revision.Jul 26 2022, 12:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 26 2022, 12:04 AM
var-const requested review of this revision.Jul 26 2022, 12:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 26 2022, 12:04 AM
Herald added a reviewer: Restricted Project. · View Herald Transcript

Use the actual patch number.

var-const updated this revision to Diff 447606.Jul 26 2022, 1:01 AM

Fix the CI and rebase.

huixie90 accepted this revision.Jul 26 2022, 6:09 AM
huixie90 added a subscriber: huixie90.
huixie90 added inline comments.
libcxx/test/std/algorithms/alg.modifying.operations/alg.generate/ranges_generate.pass.cpp
139

bit: would it be possible to pass in uninitialized array instead ?

This revision is now accepted and ready to land.Jul 26 2022, 6:09 AM
var-const marked an inline comment as done.

Address feedback and rebase.

ldionne accepted this revision.Jul 26 2022, 1:55 PM
ldionne added a subscriber: ldionne.
ldionne added inline comments.
libcxx/include/__algorithm/ranges_generate.h
47

This is a weird constraint, I would have expected _OutIter to be an output_iterator instead.

I don't think we are testing for this, and I'm not asking that you add tests for it. However, it looks like the Standard doesn't want implementations to assume that *it++ = expr is valid, and they want us to use *it = expr; ++it instead (which we do).

libcxx/include/__algorithm/ranges_generate_n.h
42

We should technically be using std::invoke here according to the concept requirements specified in the signature (all we have is invocable<_Func&>). However, I don't think there is any way to distinguish __gen() from invoke(gen) given that it's being called with no arguments. I tried finding a case but couldn't.

So, nothing to do.

huixie90 added inline comments.Jul 26 2022, 3:22 PM
libcxx/include/__algorithm/ranges_generate.h
47

in fact, lots of algorithms are constrained with x_iterator and indirectly_writable. very few of them actually use output_iterator. I wonder what is the reason behind it.

libcxx/include/__algorithm/ranges_generate_n.h
42

I was about to say the same and actually tried very hard. but could not find an example where gen() does not work while invoke(gen) works.

Although nothing to do here, I'd like to share how far I went.
You can use gen() even if gen doesn't have operator() but convertible to a function pointer.
https://godbolt.org/z/b11erWhcc

var-const updated this revision to Diff 447859.Jul 26 2022, 3:49 PM

Add comment and rebase.

This revision was landed with ongoing or failed builds.Jul 26 2022, 3:50 PM
This revision was automatically updated to reflect the committed changes.
var-const marked an inline comment as done.Jul 27 2022, 3:41 PM
var-const added inline comments.
libcxx/include/__algorithm/ranges_generate_n.h
42

Thank you! This idea is pretty ingenious, cool that calling it directly still works.