-
Notifications
You must be signed in to change notification settings - Fork 37.7k
c++20: Opt-in to modeling view and borrowed_range for Span #23226
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
Conversation
This matches the behavior of std::span, as Span satisfies the same concepts.
So the idea, as I understand, is that this adds compile-time checking capabilities a bit like rust? Yes that's really neat. |
@laanwj Yes, but in this case we're actually opting out of some of those protections because of the semantics of Span. By default, the returned iterator type for a potentially dangling reference is a The I found it useful to copy/paste a little cheat-sheet of these concepts from libstdc++'s implementation: /// [range.range] The range concept.
template<typename _Tp>
concept range = requires(_Tp& __t)
{
ranges::begin(__t);
ranges::end(__t);
};
template<typename _Tp>
concept view
= range<_Tp> && movable<_Tp> && default_initializable<_Tp>
&& enable_view<_Tp>;
// Part of the constraints of ranges::borrowed_range
template<typename _Tp>
concept __maybe_borrowed_range
= is_lvalue_reference_v<_Tp>
|| enable_borrowed_range<remove_cvref_t<_Tp>>;
template<typename _Tp>
concept borrowed_range
= range<_Tp> && __detail::__maybe_borrowed_range<_Tp>;
/// A range which can be safely converted to a view.
template<typename _Tp>
concept viewable_range = range<_Tp>
&& (borrowed_range<_Tp> || view<remove_cvref_t<_Tp>>); In the above example, enabling
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
🐙 This pull request conflicts with the target branch and needs rebase. Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a "draft". |
There hasn't been much activity lately and the patch still needs rebase. What is the status here?
|
1 similar comment
There hasn't been much activity lately and the patch still needs rebase. What is the status here?
|
Closing for now. Let us know when you are working on this again and if it should be reopened. |
This is a c++20 feature, so it's obviously not a rush to get in, but it may be useful while we play around with ranges in the codebase before actually switching to c++20.
This matches the behavior of std::span.
The below sample program demonstrates what these enable:
Compiled with:
g++-10 -O2 -std=c++2a span.cpp -o span
If
enable_view
is disabled, the first example will fail to compile.If
enable_borrowed_range
is disabled, the second will fail to compile.Sidenote:
std::ranges::dangling
is neat :)