This is an archive of the discontinued LLVM Phabricator instance.

[lib++][ranges][NFC] Refactor `iterator_operations.h` to use tags.
ClosedPublic

Authored by var-const on Jul 8 2022, 12:39 PM.

Details

Summary

Change the mechanism in iterator_operations.h to pass around a generic
policy tag indicating whether an internal function is being invoked from
a "classic" STL algorithm or a ranges algorithm. IterOps is now
a template class specialized on the policy tag.

The advantage is that this mechanism is more generic and allows defining
arbitrary conditions in a clean manner.

Also add a few more iterator functions to IterOps.

Diff Detail

Event Timeline

var-const created this revision.Jul 8 2022, 12:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 8 2022, 12:39 PM
var-const requested review of this revision.Jul 8 2022, 12:39 PM
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a reviewer: Restricted Project. · View Herald Transcript
var-const updated this revision to Diff 443341.Jul 8 2022, 1:43 PM

Address feedback.

huixie90 added inline comments.
libcxx/include/__algorithm/iterator_operations.h
68

I think you need to remove all the cv_ref instead of just the reference

68

I thought I've commented somewhere I don't seem to find it anymore. Just be aware that the type of std::move(*std::forward<_Iter>(__i)); does not always match
typename iterator_traits<typename remove_reference<_Iter>::type>::value_type&&. e.g. vector<bool>::itertor. But I think the code you've written is fine because most of the time the users do want the conversion (except for the new c++20 stuff which is not meant to be usable for classic algorithms)

var-const marked 2 inline comments as done.Jul 12 2022, 2:40 AM
var-const added inline comments.
libcxx/include/__algorithm/iterator_operations.h
68

Thanks, this is a very good point. I also think it should be okay given how it's used.

var-const updated this revision to Diff 443886.Jul 12 2022, 2:45 AM
var-const marked an inline comment as done.

Rebase on main, address feedback.

philnik accepted this revision.Jul 12 2022, 2:58 AM
philnik added a subscriber: philnik.

LGTM % nits and with green CI.

libcxx/include/__algorithm/iterator_operations.h
76

Is there a reason you don't use the customization point? AFAIK iter_swap has to be implemented properly or not at all, just like swap.

libcxx/include/__algorithm/lower_bound.h
31

_AlgPolicy?

52

There shouldn't be any underscores here.

libcxx/include/__algorithm/set_intersection.h
37

I think _AlgPolicy would be a bit better and more consistent with _RangesAlgPolicy and _ClassicAlgPolicy.

This revision is now accepted and ready to land.Jul 12 2022, 2:58 AM
huixie90 accepted this revision.Jul 12 2022, 4:58 AM
huixie90 added inline comments.
libcxx/include/__algorithm/iterator_operations.h
39

question. why double underscores here?

76

std::iter_swap is not a customization point, it simply does the two step swap (using std::swap then ADL swap).
On the other hand, std::ranges::iter_swap is a customization point where ADL iter_swap should be looked up.
(unrelated but FYI std::iter_swap is not constrained so to make std::ranges::iter_swap work probably, it needs the poison pill void iter_swap(a,b) = delete;)

Anyway I think this code is fine as std::iter_swap isn't a customization point.

philnik added inline comments.Jul 12 2022, 5:12 AM
libcxx/include/__algorithm/iterator_operations.h
39

iter_move is only used by the standard since C++20, so we can't use it in earlier versions.

76

I misread the cppreference entry as it being a customization point. Never mind then.

var-const marked 6 inline comments as done.

Address feedback, fix the CI.

libcxx/include/__algorithm/iterator_operations.h
39

Theoretically, a user could define a macro "overriding" the name unless it's a name reserved for the implementation (which iter_move isn't until C++20 like Nikolas mentioned).

var-const edited the summary of this revision. (Show Details)Jul 12 2022, 10:31 AM

Rename one remaining "tag" to "policy".

Fix the CI.