]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2019-2021 Free Software Foundation, Inc. |
6d0dff49 JW |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | ||
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | ||
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | // { dg-options "-std=gnu++2a" } | |
19 | // { dg-do compile { target c++2a } } | |
20 | ||
21 | #include <iterator> | |
22 | ||
23 | struct none; | |
24 | ||
25 | template<typename T> | |
26 | concept has_inc_traits_type | |
27 | = requires { typename std::incrementable_traits<T>::difference_type; }; | |
28 | ||
29 | // Check std::incrementable_traits<T>::difference_type is U (or doesn't exist). | |
30 | template<typename T, typename U> | |
31 | concept check_inc_traits = (has_inc_traits_type<T> != std::same_as<U, none>); | |
32 | ||
33 | static_assert( check_inc_traits<void, none> ); | |
34 | static_assert( check_inc_traits<const void, none> ); | |
35 | static_assert( check_inc_traits<void*, none> ); | |
36 | static_assert( check_inc_traits<const void*, none> ); | |
37 | ||
38 | static_assert( check_inc_traits<int, int> ); | |
39 | static_assert( check_inc_traits<const int, int> ); | |
40 | ||
41 | static_assert( check_inc_traits<int*, std::ptrdiff_t> ); | |
42 | static_assert( check_inc_traits<const int*, std::ptrdiff_t> ); | |
43 | static_assert( check_inc_traits<int[2], std::ptrdiff_t> ); | |
44 | static_assert( check_inc_traits<const int[2], std::ptrdiff_t> ); | |
45 | ||
46 | struct A { using difference_type = int; }; | |
47 | static_assert( check_inc_traits<A, int> ); | |
48 | static_assert( check_inc_traits<const A, int> ); | |
49 | struct B : private A { }; | |
50 | static_assert( check_inc_traits<B, none> ); | |
51 | ||
52 | struct C { }; | |
53 | short operator-(C, C) { return 0; } | |
54 | static_assert( check_inc_traits<C, short> ); | |
55 | static_assert( check_inc_traits<const C, short> ); | |
56 | ||
57 | struct D { }; | |
58 | unsigned short operator-(D, D) { return 0; } | |
59 | static_assert( check_inc_traits<D, short> ); | |
60 | static_assert( check_inc_traits<const D, short> ); | |
61 | ||
62 | struct E { }; | |
63 | template<> | |
64 | struct std::incrementable_traits<E> { using difference_type = long; }; | |
65 | static_assert( check_inc_traits<E, long> ); | |
66 | static_assert( check_inc_traits<const E, long> ); | |
67 | ||
68 | template<typename T> | |
69 | concept has_alias = requires { typename std::iter_difference_t<T>; }; | |
70 | ||
71 | // Check std::iter_difference_t<T> is U (or doesn't exist). | |
72 | template<typename T, typename U> | |
73 | concept check_alias = (has_alias<T> != std::same_as<U, none>); | |
74 | ||
75 | static_assert( check_alias<void, none> ); | |
76 | static_assert( check_alias<const void, none> ); | |
77 | static_assert( check_alias<void*, none> ); | |
78 | static_assert( check_alias<const void*, none> ); | |
79 | ||
80 | static_assert( check_alias<int, int> ); | |
81 | static_assert( check_alias<const int, int> ); | |
82 | static_assert( check_alias<int*, std::ptrdiff_t> ); | |
83 | static_assert( check_alias<const int*, std::ptrdiff_t> ); | |
84 | static_assert( check_alias<int[2], std::ptrdiff_t> ); | |
85 | static_assert( check_alias<const int[2], std::ptrdiff_t> ); | |
86 | ||
87 | static_assert( check_alias<A, int> ); | |
88 | static_assert( check_alias<const A, int> ); | |
89 | static_assert( check_alias<B, none> ); | |
90 | static_assert( check_alias<C, short> ); | |
91 | static_assert( check_alias<const C, short> ); | |
92 | static_assert( check_alias<D, short> ); | |
93 | static_assert( check_alias<const D, short> ); | |
94 | static_assert( check_alias<E, long> ); | |
95 | static_assert( check_alias<const E, long> ); | |
96 | ||
97 | struct F { }; | |
98 | template<> | |
99 | struct std::iterator_traits<F> { using difference_type = F; }; | |
100 | // iterator_traits<F> is specialized, so use its difference_type. | |
101 | static_assert( check_alias<F, std::iterator_traits<F>::difference_type> ); | |
102 | ||
103 | struct G { }; | |
104 | template<> | |
105 | struct std::incrementable_traits<G> { using difference_type = G; }; | |
106 | template<> | |
107 | struct std::iterator_traits<G> { using difference_type = int; }; | |
108 | // iterator_traits<G> is specialized, so use its difference_type. | |
109 | static_assert( check_alias<G, std::iterator_traits<G>::difference_type> ); | |
110 | ||
111 | struct H { }; | |
112 | template<> | |
113 | struct std::incrementable_traits<H> { using difference_type = H; }; | |
114 | template<> | |
115 | struct std::iterator_traits<H> | |
116 | { | |
117 | using iterator_category = input_iterator_tag; | |
118 | using difference_type = int; | |
119 | using value_type = char; | |
120 | using reference = value_type&; | |
121 | }; | |
122 | // iterator_traits<H> is specialized, so use its difference_type. | |
123 | static_assert( check_alias<H, std::iterator_traits<H>::difference_type> ); | |
124 | ||
125 | struct I | |
126 | { | |
127 | using difference_type = I; | |
128 | }; | |
129 | // iterator_traits<I> is not specialized, and no standard specialization | |
130 | // matches, so use incrementable_traits. | |
131 | static_assert( check_alias<I, std::incrementable_traits<I>::difference_type> ); | |
132 | ||
133 | struct J | |
134 | { | |
135 | using iterator_category = std::input_iterator_tag; | |
136 | using difference_type = int; | |
137 | using value_type = char; | |
138 | using reference = value_type&; | |
139 | }; | |
140 | // iterator_traits<J> matches constrained specialization in the library, | |
141 | // so use its difference_type. | |
142 | static_assert( check_alias<J, int> ); |