-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Implement ranges::ref_view #1132
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cedff2e
Implement ranges::ref_view
miscco dd78edb
Merge branch 'master' into ref_view
miscco 35069af
Review comments from STL
miscco aee50e9
Fix derp
miscco b4958f5
Apply Caseys review comments
miscco d8ee104
Merge branch 'master' into ref_view
miscco 296e15f
Review comments
CaseyCarter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| RUNALL_INCLUDE ..\concepts_matrix.lst |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include <algorithm> | ||
| #include <cassert> | ||
| #include <concepts> | ||
| #include <memory> | ||
| #include <ranges> | ||
| #include <span> | ||
| #include <utility> | ||
|
|
||
| #include <range_algorithm_support.hpp> | ||
| using namespace std; | ||
|
|
||
| // clang-format off | ||
| template <class Range> | ||
| concept can_empty = requires(Range& r) { ranges::empty(r); }; | ||
| template <class Range> | ||
| concept can_data = requires(Range& r) { ranges::data(r); }; | ||
| template <class Range> | ||
| concept can_size = requires(Range& r) { ranges::size(r); }; | ||
| // clang-format on | ||
|
|
||
| struct instantiator { | ||
| template <ranges::range R> | ||
| static constexpr void call() { | ||
| using ranges::ref_view, ranges::begin, ranges::end, ranges::forward_range; | ||
| int input[3] = {0, 1, 2}; | ||
|
|
||
| { // traits | ||
| STATIC_ASSERT(ranges::input_range<R> || ranges::output_range<R, const int&>); | ||
| STATIC_ASSERT(ranges::enable_borrowed_range<ref_view<R>>); | ||
| } | ||
miscco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| { // constructors and assignment operators | ||
| STATIC_ASSERT(!constructible_from<ref_view<R>, R>); | ||
|
|
||
| ref_view<R> default_constructed{}; | ||
| STATIC_ASSERT(is_nothrow_default_constructible_v<ref_view<R>>); | ||
|
|
||
| R wrapped_input{input}; | ||
| ref_view<R> same_range{wrapped_input}; | ||
| STATIC_ASSERT(is_nothrow_constructible_v<ref_view<R>, R&>); | ||
|
|
||
| auto copy_constructed = same_range; | ||
| if constexpr (forward_range<R>) { | ||
| assert(copy_constructed.begin().peek() == begin(input)); | ||
| } | ||
| assert(copy_constructed.end().peek() == end(input)); | ||
|
|
||
| default_constructed = copy_constructed; | ||
| if constexpr (forward_range<R>) { | ||
| assert(default_constructed.begin().peek() == begin(input)); | ||
| } | ||
| assert(default_constructed.end().peek() == end(input)); | ||
|
|
||
| [[maybe_unused]] auto move_constructed = std::move(default_constructed); | ||
| if constexpr (forward_range<R>) { | ||
| assert(move_constructed.begin().peek() == begin(input)); | ||
| } | ||
| assert(move_constructed.end().peek() == end(input)); | ||
|
|
||
| same_range = std::move(copy_constructed); | ||
| if constexpr (forward_range<R>) { | ||
| assert(same_range.begin().peek() == begin(input)); | ||
| } | ||
| assert(same_range.end().peek() == end(input)); | ||
| } | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| { // access | ||
| R wrapped_input{input}; | ||
| ref_view<R> test_view{wrapped_input}; | ||
| same_as<R> auto& base_range = as_const(test_view).base(); | ||
| assert(addressof(base_range) == addressof(wrapped_input)); | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| STATIC_ASSERT(noexcept(as_const(test_view).base())); | ||
| } | ||
|
|
||
| { // iterators | ||
| R wrapped_input{input}; | ||
| ref_view<R> test_view{wrapped_input}; | ||
| const same_as<ranges::iterator_t<R>> auto first = as_const(test_view).begin(); | ||
| assert(first.peek() == input); | ||
| STATIC_ASSERT(noexcept(as_const(test_view).begin()) == noexcept(wrapped_input.begin())); | ||
|
|
||
| const same_as<ranges::sentinel_t<R>> auto last = as_const(test_view).end(); | ||
| assert(last.peek() == end(input)); | ||
| STATIC_ASSERT(noexcept(as_const(test_view).end()) == noexcept(wrapped_input.end())); | ||
| } | ||
|
|
||
| { // state | ||
| STATIC_ASSERT(can_size<ref_view<R>> == ranges::sized_range<R>); | ||
| if constexpr (ranges::sized_range<R>) { | ||
| R wrapped_input{input}; | ||
| ref_view<R> test_view{wrapped_input}; | ||
|
|
||
| const same_as<ranges::range_size_t<R>> auto ref_size = as_const(test_view).size(); | ||
| assert(ref_size == size(wrapped_input)); | ||
|
|
||
| STATIC_ASSERT(noexcept(as_const(test_view).size()) == noexcept(wrapped_input.size())); | ||
| } | ||
|
|
||
| STATIC_ASSERT(can_data<ref_view<R>> == ranges::contiguous_range<R>); | ||
| if constexpr (ranges::contiguous_range<R>) { | ||
| R wrapped_input{input}; | ||
| ref_view<R> test_view{wrapped_input}; | ||
|
|
||
| const same_as<int*> auto ref_data = as_const(test_view).data(); | ||
| assert(ref_data == input); | ||
|
|
||
| STATIC_ASSERT(noexcept(as_const(test_view).data()) == noexcept(wrapped_input.data())); | ||
| } | ||
|
|
||
| STATIC_ASSERT(can_empty<ref_view<R>> == can_empty<R>); | ||
| if constexpr (can_empty<R>) { | ||
| R wrapped_input{input}; | ||
| ref_view<R> test_view{wrapped_input}; | ||
|
|
||
| const same_as<bool> auto ref_empty = as_const(test_view).empty(); | ||
| assert(!ref_empty); | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| STATIC_ASSERT(noexcept(as_const(test_view).empty()) == noexcept(ranges::empty(wrapped_input))); | ||
|
|
||
| R empty_range{}; | ||
| ref_view<R> empty_view{empty_range}; | ||
| assert(empty_view.empty()); | ||
| } | ||
| } | ||
|
|
||
| { // CTAD | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| span<const int, 3> spanInput{input}; | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ref_view span_view{spanInput}; | ||
CaseyCarter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| STATIC_ASSERT(same_as<decltype(span_view), ref_view<span<const int, 3>>>); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| int main() { | ||
| STATIC_ASSERT((test_inout<instantiator, int>(), true)); | ||
| test_inout<instantiator, int>(); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I observe that the
requiressaysstatic_cast<_OtherRng&&>(_Other)instead of_STD forward<_OtherRng>(_Other). I am not sure if the variation is intentional. No change requested.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional: I try to avoid instantiating and/or specializing templates during overload resolution for throughput. In this case, that means
static_cast<T&&>(t)in the constraints instead ofstd::forward<T>(t)that is used innoexcept-specifiers and function template bodies which are instantiated after overload resolution. There's an argument to be made that the difference is anomalous enough to offset any readability benefit.