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
Add size_type, difference_type to span #387
Conversation
Out of curiosity, could you give an example of what exactly this is fixing. Is the example something that could be added to the unit tests? |
Sure. I was writing some super generic code with the goal of using almost always auto together with types to make a function that performs actions on segments of a container (such as span, various flat sets, vector, etc). Works fine on standard libraries, but there is no container traits to get size_type or difference_type for looping (and since its a partition function, you need actual integral types, not just an iterated loop).
I'm not exactly sure how to turn this into a test given it was a compile error. |
After checking, string_view and the proposed array_view (not sure if that made a TS or not, http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0122r0.pdf) and the proposed span (http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0122r3.pdf) aren't technically containers, and neither is gsl::span because they don't control their own memory. This seems awful given the proliferation of container like things in the future standard. However, both string_view, array_view, and span implement the full container concept in their standards documents even though they are not actually containers. I'd say the trend seems to be that "container-like" things/views should follow the container concept when possible. (Really it seems like there needs to be some view concepts). I could use size() to get the size_type, and then use iterator and iterator traits to get difference_type. This is ugly though and still really contains an implicit reliance on the container concept giving iterator; you really can't get away from it sneaking in. |
Actually I'm wrong, proposed span does not have size_type. It does have difference type. Maybe this should be changed? |
General note: |
I don't know that I agree with that. If you can have a function that works with a const vector, it should probably work out of the box with a span. There is at least one major bug here: difference_type is specifically included in P0122R3 for precisely this reason and this repository is supposed to be a reference implementation of that according to the paper. size_type isn't mentioned. I'm assuming oversight, but maybe there is a reason? Maybe @neilmacintosh can weigh in? |
@garyfurnish Sorry to let this thread run so long, but I was busy elsewhere today.
In the current standard library, Both of these positions met with approval from the Library Evolution Working Group in standardization meetings (in fact, not having Hope that helps to clear things up. |
@neilmacintosh That actually really clarifies things on size_type (and makes sense too). What about difference_type? |
Currently you can't use span generically as a replacement for std containers because it lacks size_type and difference_type, unlike std containers.