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

generate seems broken with move-only return types #905

Closed
mikezackles opened this issue Oct 10, 2018 · 3 comments · Fixed by #909
Closed

generate seems broken with move-only return types #905

mikezackles opened this issue Oct 10, 2018 · 3 comments · Fixed by #909
Labels

Comments

@mikezackles
Copy link
Contributor

I haven't been able to get anything that uses this view to compile:

auto rng = view::generate_n([]()->MoveOnlyString{ return {"Hello, world!"}; }, 4);

godbolt

The issue seems to revolve around the cached value stored by view::generate_n (and view::generate).

To me this seems like an important use case, but hopefully I'm just misunderstanding something!

@mikezackles
Copy link
Contributor Author

FWIW, I can work around this with something like this:

auto rng = view::repeat_n(0, 4) | view::transform([](auto)->MoveOnlyString{ return {"Hello, world!"}; });

@ericniebler
Copy link
Owner

ericniebler commented Oct 11, 2018

FYI, the reason that your workaround works is because view::transform assumes that transformation function is a regular function; that is, that different invocations of it with the same argument yield the same result. The generate views can't make that assumption, and so needs to cache the value in order to satisfy the semantic requirements of the InputIterator concept: namely, that when evaluated multiple types without an intervening increment, *i always yields the same value.

@mikezackles
Copy link
Contributor Author

Cool, thanks for the clarification and the quick fix! I was trying something similar but couldn't manage to get it quite right.

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

Successfully merging a pull request may close this issue.

2 participants