diff options
author | Jonathan Wakely <jwakely@redhat.com> | 2025-03-26 11:47:05 +0000 |
---|---|---|
committer | Jonathan Wakely <redi@gcc.gnu.org> | 2025-04-04 12:56:59 +0100 |
commit | 03ac8886e5c1fa16da90276fd721a57fa9435f4f (patch) | |
tree | 1f75de5dd73ff9393f2937a7dd21b1ca54b7600d /libstdc++-v3/testsuite | |
parent | 84d668b0ca67c5d3fe6430f101d61e60aa796a81 (diff) | |
download | gcc-03ac8886e5c1fa16da90276fd721a57fa9435f4f.zip gcc-03ac8886e5c1fa16da90276fd721a57fa9435f4f.tar.gz gcc-03ac8886e5c1fa16da90276fd721a57fa9435f4f.tar.bz2 |
libstdc++: Replace use of __mindist in ranges::uninitialized_xxx algos [PR101587]
In r15-8980-gf4b6acfc36fb1f I introduced a new function object for
finding the smaller of two distances. In bugzilla Hewill Kang pointed
out that we still need to explicitly convert the result back to the
right difference type, because the result might be an integer-like class
type that doesn't convert to an integral type explicitly.
Rather than doing that conversion in the __mindist function object, I
think it's simpler to remove it again and just do a comparison and
assignment. We always want the result to have a specific type, so we can
just check if the value of the other type is smaller, and then convert
that to the other type if so.
libstdc++-v3/ChangeLog:
PR libstdc++/101587
* include/bits/ranges_uninitialized.h (__detail::__mindist):
Remove.
(ranges::uninitialized_copy, ranges::uninitialized_copy_n)
(ranges::uninitialized_move, ranges::uninitialized_move_n): Use
comparison and assignment instead of __mindist.
* testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc:
Check with ranges that use integer-like class type for
difference type.
* testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc:
Likewise.
Reviewed-by: Tomasz Kaminski <tkaminsk@redhat.com>
Reviewed-by: Hewill Kang <hewillk@gmail.com>
Diffstat (limited to 'libstdc++-v3/testsuite')
-rw-r--r-- | libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc | 30 | ||||
-rw-r--r-- | libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc | 29 |
2 files changed, 57 insertions, 2 deletions
diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc index af3b733..5dff0da 100644 --- a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_copy/constrained.cc @@ -178,12 +178,40 @@ test03() void test_pr101587() { - short in[1]; + short in[1]{}; __gnu_test::test_contiguous_range r(in); // difference_type is integer-like long out[1]; std::span<long> o(out); // difference_type is ptrdiff_t ranges::uninitialized_copy(r, o); ranges::uninitialized_copy_n(ranges::begin(r), 0, o.begin(), o.end()); + + // iterator that has an integer-like class type for difference_type + struct Iter + { + using value_type = long; + using difference_type = std::ranges::__detail::__max_diff_type; + + long& operator*() const { return *p; } + + Iter& operator++() { ++p; return *this; } + Iter operator++(int) { return Iter{p++}; } + + difference_type operator-(Iter i) const { return p - i.p; } + bool operator==(const Iter&) const = default; + + long* p = nullptr; + }; + static_assert(std::sized_sentinel_for<Iter, Iter>); + + std::ranges::subrange<Iter> rmax(Iter{out+0}, Iter{out+1}); + // Check with integer-like class type for output range: + std::ranges::uninitialized_copy(in, rmax); + std::ranges::uninitialized_copy_n(in+0, 1, rmax.begin(), rmax.end()); + + int to[1]; + // And for input range: + std::ranges::uninitialized_copy(rmax, to); + std::ranges::uninitialized_copy_n(rmax.begin(), 1, to+0, to+1); } int diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc index fe82d1f1..3e81244 100644 --- a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc @@ -188,12 +188,39 @@ test03() void test_pr101587() { - short in[1]; + short in[1]{}; __gnu_test::test_contiguous_range r(in); // difference_type is integer-like long out[1]; std::span<long> o(out); // difference_type is ptrdiff_t ranges::uninitialized_move(r, o); ranges::uninitialized_move_n(ranges::begin(r), 0, o.begin(), o.end()); + + struct Iter + { + using value_type = long; + using difference_type = std::ranges::__detail::__max_diff_type; + + long& operator*() const { return *p; } + + Iter& operator++() { ++p; return *this; } + Iter operator++(int) { return Iter{p++}; } + + difference_type operator-(Iter i) const { return p - i.p; } + bool operator==(const Iter&) const = default; + + long* p = nullptr; + }; + static_assert(std::sized_sentinel_for<Iter, Iter>); + + std::ranges::subrange<Iter> rmax(Iter{out+0}, Iter{out+1}); + // Check with integer-like class type for output range: + std::ranges::uninitialized_move(in, rmax); + std::ranges::uninitialized_move_n(in+0, 1, rmax.begin(), rmax.end()); + + int to[1]; + // And for input range: + std::ranges::uninitialized_copy(rmax, to); + std::ranges::uninitialized_copy_n(rmax.begin(), 1, to+0, to+1); } int |