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
[iterator.operations] std::distance
may be missing a precondition
#6822
Comments
std::distance
may be missing a preconditionstd::distance
may be missing a precondition
Another thing that strike me as odd is that If
... doesn't that imply that |
There's nothing editorial here. Please file an LWG issue for std::distance. I don't see a problem for max_size though. Any precondition for that seems to be a requirement on the implementation: don't be stupid. It would be silly for an implementation to define the container's iterator's difference type to be unable to represent the largest container. Or in other words, max_size should be defined so that max_size is always valid and has no precondition. Allowing a container to have a maximum size that can't be returned by max_size makes no sense. I don't know why the core language allows |
Alright, thanks, I've done that. I wanted to make sure this is non-editorial because I don't 100% understand the conventions behind Preconditions yet; there would have been a chance that it's all OK as is.
I mean sure, it would be stupid. I was just curious whether it's technically permitted in which case
Presumably, it would mean that on an 8-bit architecture with 8-bit pointers, you can have 200-byte arrays. The lack of pointer difference guarantees doubles the maximum object size. |
If you think that, then it may be better to rephrase it along the lines of: -Returns: distance(begin(), end()) for the largest possible container.
+Returns: The size of the largest possible container,
+which shall be low enough to ensure that distance(begin(), end()) is well-defined. If you really think this is already implied by the lack of preconditions, this would be purely editorial. However, it doesn't feel very editorial to me. |
The implementation isn't actually required to construct the largest possible container and then call distance(begin(), end()). It can just return the value. And if the value can't be returned, the implementation is broken. We don't need a precondition saying "this function is not undefined", and that wouldn't be a precondition on the user anyway.
There cannot be a precondition for max_size. How would you ever verify that precondition to be sure it was safe to call it? You would have to check that the difference type could represent the return value of the function you want to call, a value which you can't know without calling it.
You can have that anyway, use a 16-bit types for ptrdiff_t (int must be at least 16 bits anyway). |
Yeah fair point; perhaps a more relevant example would be Is GCC actually limited to 32 KiB on 16-bit targets and 2 GiB on 32-bit targets, or does it leverage this additional freedom? I'd find it surprising if no C++ compiler ever made use of this. |
You can check whether |
It seems that we shouldn't specify Perhaps |
No editorial work required (but normative work needed https://cplusplus.github.io/LWG/issue4055), I consider this done. |
https://eel.is/c++draft/iterator.operations#lib:distance
How come there is no precondition that the distance can be represented using the result type? There are a number of ways that it can happen:
difference_type
.std::ptrdiff_t
cannot represent the pointer difference(last - first)
.How can the effect be
(last - first)
for random access iterators, but this isn't always well-defined, and it isn't included in the Preconditions paragraph for this function?Is this a LWG defect, an editorial issue, or a non-issue and I'm just missing something obvious?
The text was updated successfully, but these errors were encountered: