Following on from my previous question, can we prove that the standard allows us to pass an empty range to a standard algorithm?
Paragraph 24.1/7 defines an “empty range” as the range [i,i) (where i is valid), and i would appear to be “reachable” from itself, but I’m not sure that this qualifies as a proof.
In particular, we run into trouble when looking at the sorting functions. For example, std::sort:
Complexity:
O(N log(N))(whereN==last–first) comparisons
Since log(0) is generally considered to be undefined, and I don’t know what 0*undefined is, could there be a problem here?
(Yes, ok, I’m being a bit pedantic. Of course no self-respecting stdlib implementation would cause a practical problem with an empty range passed to std::sort. But I’m wondering whether there’s a potential hole in the standard wording here.)
I don’t seem much room for question. In §24.1/6 we’re told:
and in $24.1/7:
Since
0is finite,[i, i)is a valid range. §24.1/7 goes on to say:That doesn’t go quite so far as to say that a valid range guarantees defined results (reasonable, since there are other requirements, such as on the comparison function) but certainly seems to imply that a range being empty, in itself, should not lead to UB or anything like that. In particular, however, the standard makes an empty range just another valid range; there’s no real differentiation between empty and non-empty valid ranges, so what applies to a non-empty valid range applies equally well to an empty valid range.