-
Notifications
You must be signed in to change notification settings - Fork 86
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
Order the arguments of pointer_in_range #171
base: develop
Are you sure you want to change the base?
Conversation
Semantically ordered arguments should be lexically ordered, too. See https://quuxplusone.github.io/blog/2021/05/05/its-clamping-time/
I strongly disagree. It is established convention that iterators denoting a range are passed in pairs of arguments, begin and end. Breaking that pair with another argument will be a constant source of confusion and mistakes.
|
BTW, |
I do agree that if the function were actually FWIW, |
Sorry, I don't see why, aside from personal preference. IMO, the least confusing interface is "good", and the least confusing most of the time means the most consistent with other similar interfaces. "a <= b < c" doesn't exist in C++, so trying to introduce it now, and not even in that literal form but a remote shadow of it in the form of the order of arguments, does not make the interface consistent or any less error-prone at all. It just makes that interface an odd ball to stumble upon.
Given that most if not all std interfaces accepting pairs of iterators accept them as consecutive arguments, the pivot iterator in the middle is confusing, at least at first, until you remember that |
There are at least two reasons to prefer the current argument order. First, in the sentence "the pointer p is in the range [b, e)" the order is p, b, e. (This is consistent with is_base_of.) Second, the mathematical notation for "p is a member of [b, e)" is p ∈ [b, e). This is not the same as std::rotate, which takes two consecutive ranges (which also necessarily form a valid range when concatenated.) |
Though the current reflector discussion on LEWG about P3234R0 has focused on |
Just stumbled onto this by accident, so apologies for discussing this 7 months after last comment but I think it would be great if we would have overload that enables you to write:
I think this is a new convention in |
An overload with span in Core seems OK. I might end up adding that anyway since it looks like the desire is there for P3234R1 to use that interface too. |
Semantically ordered arguments should be lexically ordered, too.
See https://quuxplusone.github.io/blog/2021/05/05/its-clamping-time/
I feel very strongly that the arguments to
pointer_in_range
— like the arguments torotate
and so on (with the notable, regrettable, exception ofstd::clamp
) — should be given in "semantic order." This means thatpointer_in_range(a, b, c)
should betrue
iffa <= b < c
.With the current definition, the programmer has to do a lot of extra brainwork (i.e. consult the documentation) to figure out that
pointer_in_range(a, b, c)
istrue
iffa >= b && a < c
. (As opposed, to, say, ifc >= a && c < b
.) Putting the "middle" argument in the "middle" is simply the most natural and self-explanatory choice.The changes in the test file should speak for themselves: we see that it's only the "middle" argument that's varying, and that the expected return value is
false
only when the "middle" value incorrectly falls outside the "lower" and "higher" bounds.I'm sending an email to this effect also to the LEWG reflector, strongly recommending that the same change be made in P3234R0.
Obviously this will be much harder to do after Boost 1.86 ships, so time is of the essence now.