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

[alg.search] search_n. Use consistent wording with search #3261

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

andreyfe1
Copy link

Hi,
For the search_n algorithm the Standard says:

Returns: The first iterator i in the range [first, last-count) such that for every non-negative integer n less than count the following corresponding conditions hold: (i + n) == value, pred((i+ n),value) != false. Returns last if no such iterator is found.

It seems that if n>=0 and count <=0 we don't have a chance to apply a predicate. So, we won't find "such iterator". According to this logic last should be returned. However, libcxx and Microsoft's C++ Standard Library returns first. According to the issue there was a different interpretation of search_n wording.

Moreover, search returns first in the same case. I hope it's just an editorial issue.

@jensmaurer
Copy link
Member

Please squash and force-push your commits. Also, prefix your commit comment with "[alg.search]".

That said, there is explicit normative wording for "search" that we get "first1" for an empty second range. This wording is missing from "search_n", although the rest of the description has the same structure. I can't see how this issue would be editorial.

@jensmaurer jensmaurer added the lwg Issue must be reviewed by LWG. label Oct 2, 2019
@andreyfe1
Copy link
Author

The patch is only for clarification what should be returned from "search_n". I see "first" as a return value is obvious for standard libraries, so, let's document it anyhow to avoid misinterpretation

@jensmaurer
Copy link
Member

"obvious for standard libraries": Apparently not, otherwise microsoft/STL#148 wouldn't be tagged "LWG issue needed".

@@ -3998,7 +3998,8 @@
such that for every non-negative integer \tcode{n} less than \tcode{count}
the following corresponding conditions hold:
\tcode{*(i + n) == value, pred(*(i + n),value) != false}.
Returns \tcode{last} if no such iterator is found.
Returns \tcode{first} if \tcode{count <= 0},
Copy link
Contributor

Choose a reason for hiding this comment

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

This normatively duplicates the stated behavior of the first sentence. If we want to clarify the meaning of the first sentence in the count <= 0 case, then this should be a note.

Copy link
Member

@jensmaurer jensmaurer Oct 4, 2019

Choose a reason for hiding this comment

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

And why is this extra provision present for search (without _n)? Given the similar structure, I'd expect the "normative duplication" would apply to both.

Copy link
Contributor

Choose a reason for hiding this comment

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

I consider it a defect that the redundant provision is present for std::search. Notably none of std::ranges::search, std::search_n, nor std::ranges::search_n have this defect. Spreading the provision from one member of this set to a second does not harmonize the wording of all four.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks. So, we should have an LWG issue to remove the duplication from search and maybe add a note everywhere talking about the "empty range" case. Unless @zygoloid this approach would be editorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lwg Issue must be reviewed by LWG. needs rebase The pull request needs a git rebase to resolve merge conflicts.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants