Skip to content

Add ranges::{starts,ends}_with#2618

Open
akukanov wants to merge 3 commits intomainfrom
dev/ranges-starts-ends-with-akukanov
Open

Add ranges::{starts,ends}_with#2618
akukanov wants to merge 3 commits intomainfrom
dev/ranges-starts-ends-with-akukanov

Conversation

@akukanov
Copy link
Copy Markdown
Contributor

@akukanov akukanov commented Mar 16, 2026

The implementations of ranges::starts_/ends_with redirect to mismatch and equal respectively, as described in the C++ standard.

@akukanov akukanov added this to the 2022.13.0 milestone Mar 16, 2026
@akukanov akukanov force-pushed the dev/ranges-starts-ends-with-akukanov branch 2 times, most recently from bd94959 to 928c7f5 Compare March 19, 2026 14:34
@akukanov akukanov force-pushed the dev/ranges-starts-ends-with-akukanov branch from 928c7f5 to 5625aa6 Compare March 25, 2026 12:36
_Size __n1 = std::ranges::size(__r1);
_Size __n2 = std::ranges::size(__r2);
return !(__n1 < __n2) && oneapi::dpl::ranges::equal(std::forward<_ExecutionPolicy>(__exec),
std::views::all(__r1) | std::views::drop(__n1 - __n2),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is views::all deliberately used instead of ranges::ref_view, which is shown in the standard? If yes, what is the motivation?

Copy link
Copy Markdown
Contributor Author

@akukanov akukanov Mar 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the standard,

Given a subexpression E, the expression views​::​all(E) is expression-equivalent to:

  • decay-copy(E) if the decayed type of E models view.
  • Otherwise, ref_view{E} if that expression is well-formed.
  • Otherwise, owning_view{E}.

In our case, ref_view{__r1} is valid (it can be formally proven based on the definition of the ref_view constructor), so the 3rd option is never considered. And that I think makes the use of views::all both appropriate and beneficial, as it avoids creating an extra indirection in case __r1 is already a view, and it "simplifies" the expression syntax via "pipelining".

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

views::all is almost always good enough, in my opinion unless you explicitly want a view with reference-like semantics or specifically want owning semantics.

Also, Alexey is right in making a point that all_t does not create any indirection, when possible. This technique is used in std::ranges deduction guides all over the place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants