k***@gmail.com
2018-10-16 21:23:32 UTC
Hi,
P1223R0 proposes to add various overloads of these new functions to
<algorithm> to operate upon bidirectional iterators or ranges composed
thereof:
find_backward(), find_not_backward(), find_if_backward(),
find_if_not_backward(), find_not().
I have two suggestions:
First, there is no reason why these functions shouldn't have a
specialization that produces the obvious result when operating on a forward
iterator range (that is not bidirectional). E.g. a possible implementation
for this specialization of find_backward() that returns an iterator
addressing the *last* element comparing equal to the search value if any
are present, otherwise the end-of-range:
template <typename ForwardIterator, typename Sentinel, typename T>
auto find_backward(ForwardIterator begin, Sentinel end, const T & val)
{
ForwardIterator result = end;
while (begin != end) {
if (val == *begin)
result = begin;
++begin;
}
return result;
}
Second: respectfully I suggest that for brevity (as well as for symmetry
with std::basic_string member functions), std::find_backward() might be
named std::rfind() instead. This would also fix any misleading impression
that the above-suggested forward iterator specialization was somehow
traversing the range backward.
Perfect symmetry with std::basic_string is probably not desirable, since it
would imply that find_not_backward() ought to be given the terribly clunky
name find_last_not_of(), and similar. I would suggest instead:
find_backward --> rfind
find_not_backward --> rfind_not
find_if_backward --> rfind_if
find_if_not_backward --> rfind_if_not
[find_not remains as-is]
Thanks for considering,
P1223R0 proposes to add various overloads of these new functions to
<algorithm> to operate upon bidirectional iterators or ranges composed
thereof:
find_backward(), find_not_backward(), find_if_backward(),
find_if_not_backward(), find_not().
I have two suggestions:
First, there is no reason why these functions shouldn't have a
specialization that produces the obvious result when operating on a forward
iterator range (that is not bidirectional). E.g. a possible implementation
for this specialization of find_backward() that returns an iterator
addressing the *last* element comparing equal to the search value if any
are present, otherwise the end-of-range:
template <typename ForwardIterator, typename Sentinel, typename T>
auto find_backward(ForwardIterator begin, Sentinel end, const T & val)
{
ForwardIterator result = end;
while (begin != end) {
if (val == *begin)
result = begin;
++begin;
}
return result;
}
Second: respectfully I suggest that for brevity (as well as for symmetry
with std::basic_string member functions), std::find_backward() might be
named std::rfind() instead. This would also fix any misleading impression
that the above-suggested forward iterator specialization was somehow
traversing the range backward.
Perfect symmetry with std::basic_string is probably not desirable, since it
would imply that find_not_backward() ought to be given the terribly clunky
name find_last_not_of(), and similar. I would suggest instead:
find_backward --> rfind
find_not_backward --> rfind_not
find_if_backward --> rfind_if
find_if_not_backward --> rfind_if_not
[find_not remains as-is]
Thanks for considering,
--
Kevin B. McCarty
<***@gmail.com>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0ea76761-bba3-4618-877a-b6d714f3a893%40isocpp.org.
Kevin B. McCarty
<***@gmail.com>
--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/0ea76761-bba3-4618-877a-b6d714f3a893%40isocpp.org.