Complete the random access iterator interface of dyn_array_iterator - #1272
Complete the random access iterator interface of dyn_array_iterator#1272Jeff Lenamon (lenamonj) wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
The code changes appear correct and well-covered by tests, with only a minor test-file comment needing alignment with the new includes.
Pull request overview
This PR completes the details::dyn_array_iterator random-access iterator surface so that its declared std::random_access_iterator_tag is accurate and standard algorithms (notably std::sort) compile and work correctly with gsl::dyn_array.
Changes:
- Make
dyn_array_iteratordefault-constructible unconditionally (per forward-iterator requirements across supported standards). - Add missing random-access iterator operators: relational comparisons (
<,>,<=,>=),operator->, andn + it. - Add unit tests covering the new operators,
std::sortondyn_array, and default-construction of iterator types.
File summaries
| File | Description |
|---|---|
| include/gsl/dyn_array | Adds missing random-access iterator operators and makes the iterator default constructor unconditional. |
| tests/dyn_array_tests.cpp | Adds tests for the new iterator operators, std::sort, and default-construction of iterators. |
Review details
Suppressed comments (1)
tests/dyn_array_tests.cpp:17
- The comment about testing transitive inclusion is now inaccurate: this file explicitly includes , but the comment says is not included directly. Please update the comment (or remove it) so it matches the current include list and test intent.
#include <utility>
// Despite using <algorithm> and <ranges> utilities in this test, they
// are not being included directly by this file as a test to ensure
// transitive inclusion via <gsl/dyn_array>.
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Carson Radtke (carsonRadtke)
left a comment
There was a problem hiding this comment.
The changes LGTM, but I'd like to see a couple of things added:
- Can we add a static_assert to the test suite to make sure that the iterator satisfies the random_access_iterator concept?
- Can we add a test to confirm that a
std::spancan be made from agsl::dyn_array? This requiresstd::contiguous_iterator, so we may need to adapt the implementation.
398cc33 to
0d03ab7
Compare
|
Both added.
The span test checks Built and tested at C++14, 17, 20 and 23 with gcc and clang; the span test is compiled out below 20. |
There was a problem hiding this comment.
🟡 Changes recommended
to_address performs undefined arithmetic for default and empty iterators.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 1
- Review effort level: Balanced
| using element_type = Type; | ||
| using difference_type = ptrdiff_t; | ||
|
|
||
| static constexpr element_type* to_address(const pointer i) noexcept { return i._ptr + i._pos; } |
Fixes #1270.
dyn_array_iteratordeclaresrandom_access_iterator_tagbut had no<,>,<=,>=,->orn + it, and its default constructor was guarded on__cpp_lib_ranges.std::sortover adyn_arraydid not compile. The relational operators check the same contract asoperator==,->checks the same as*,n + itforwards toit + n, and the default constructor is unconditional, as the forward iterator requirements ask in every supported standard.Two tests cover each new operator,
std::sort, and default construction of both iterator types; they fail to compile on main.