Debian Codegraph @

libstdc++-v3/testsuite/24_iterators/move_iterator/lwg3736.cc

raw
1 // { dg-do compile { target c++20 } }
2
3 // 3736. move_iterator missing disable_sized_sentinel_for specialization
4
5 #include <iterator>
6
7 template<typename Iter> using MoveIter = std::move_iterator<Iter>;
8
9 using std::sized_sentinel_for;
10 using std::disable_sized_sentinel_for;
11
12 // These assertions always passed, even without LWG 3736:
13 static_assert(sized_sentinel_for<MoveIter<int*>, MoveIter<int*>>);
14 static_assert(sized_sentinel_for<MoveIter<int*>, MoveIter<const int*>>);
15 static_assert(not sized_sentinel_for<MoveIter<int*>, MoveIter<long*>>);
16 static_assert(not sized_sentinel_for<MoveIter<int*>, std::default_sentinel_t>);
17 static_assert(not disable_sized_sentinel_for<MoveIter<int*>, MoveIter<int*>>);
18
19 // These types don't satisfy sized_sentinel_for anyway (because the subtraction
20 // is ill-formed) but LWG 3736 makes the variable template explicitly false:
21 static_assert(disable_sized_sentinel_for<MoveIter<int*>, MoveIter<long*>>);
22
23 struct Iter
24 {
25 using iterator_category = std::random_access_iterator_tag;
26 using value_type = int;
27 using pointer = int*;
28 using reference = int&;
29 using difference_type = long;
30
31 Iter() = default;
32 Iter& operator++();
33 Iter operator++(int);
34 Iter& operator--();
35 Iter operator--(int);
36 reference operator*() const;
37 pointer operator->() const;
38 Iter& operator+=(difference_type);
39 Iter& operator-=(difference_type);
40 friend Iter operator+(Iter, difference_type);
41 friend Iter operator+(difference_type, Iter);
42 friend Iter operator-(Iter, difference_type);
43 friend difference_type operator-(Iter, Iter);
44 bool operator==(Iter) const;
45 };
46
47 // Specialize the variable template so that Iter is not its own sized sentinel:
48 template<> constexpr bool std::disable_sized_sentinel_for<Iter, Iter> = true;
49 static_assert( not sized_sentinel_for<Iter, Iter> );
50
51 // LWG 3736 means that affects std::move_iterator<Iter> as well:
52 static_assert( not sized_sentinel_for<MoveIter<Iter>, MoveIter<Iter>> );