| 1 | // { dg-do compile { target c++20 } } |
| 2 | |
| 3 | // Public domain testcase from Casey Carter, send to LWG list on 2021-07-24. |
| 4 | // |
| 5 | // Here's a compile-only test case for which n is INT_MIN, which will overflow |
| 6 | // if simply negated to get |n|: https://godbolt.org/z/M7Wz1nW58. |
| 7 | |
| 8 | #include <cassert> |
| 9 | #include <iterator> |
| 10 | #include <limits> |
| 11 | |
| 12 | struct I { |
| 13 | using difference_type = int; |
| 14 | using value_type = int; |
| 15 | |
| 16 | int x; |
| 17 | |
| 18 | constexpr int operator*() const { return x; } |
| 19 | constexpr I& operator++() { ++x; return *this; } |
| 20 | constexpr I operator++(int) { ++x; return {x - 1}; } |
| 21 | constexpr bool operator==(const I&) const = default; |
| 22 | |
| 23 | constexpr int operator-(const I& that) const { return x - that.x; } |
| 24 | |
| 25 | constexpr I& operator--() { --x; return *this; } |
| 26 | constexpr I operator--(int) { --x; return {x - 1}; } |
| 27 | }; |
| 28 | static_assert(std::bidirectional_iterator<I>); |
| 29 | static_assert(std::sized_sentinel_for<I, I>); |
| 30 | |
| 31 | constexpr bool test() { |
| 32 | using L = std::numeric_limits<int>; |
| 33 | I i{-2}; |
| 34 | return std::ranges::advance(i, L::min(), I{-4}) == L::min() + 2; |
| 35 | } |
| 36 | static_assert(test()); |