]>
Commit | Line | Data |
---|---|---|
6d0dff49 JW |
1 | // Copyright (C) 2019 Free Software Foundation, Inc. |
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_readable_traits_type | |
27 | = requires { typename std::readable_traits<T>::value_type; }; | |
28 | ||
29 | // Check std::readable_traits<T>::value_type is U (or doesn't exist). | |
30 | template<typename T, typename U> | |
31 | concept check_readable_traits | |
32 | = (has_readable_traits_type<T> != std::same_as<U, none>); | |
33 | ||
34 | static_assert( check_readable_traits<void, none> ); | |
35 | static_assert( check_readable_traits<const void, none> ); | |
36 | static_assert( check_readable_traits<void*, none> ); | |
37 | static_assert( check_readable_traits<const void*, none> ); | |
38 | ||
39 | static_assert( check_readable_traits<int, none> ); | |
40 | static_assert( check_readable_traits<const int, none> ); | |
41 | ||
42 | static_assert( check_readable_traits<int*, int> ); | |
43 | static_assert( check_readable_traits<const int*, int> ); | |
44 | static_assert( check_readable_traits<int[2], int> ); | |
45 | static_assert( check_readable_traits<const int[2], int> ); | |
46 | ||
47 | struct A { using value_type = int; }; | |
48 | static_assert( check_readable_traits<A, int> ); | |
49 | static_assert( check_readable_traits<const A, int> ); | |
50 | struct B : private A { }; | |
51 | static_assert( check_readable_traits<B, none> ); | |
52 | ||
53 | struct C { }; | |
54 | short operator-(C, C) { return 0; } | |
55 | static_assert( check_readable_traits<C, none> ); | |
56 | static_assert( check_readable_traits<const C, none> ); | |
57 | ||
58 | struct D { long operator*() const { return 1L; } }; | |
59 | unsigned short operator-(D, D) { return 0; } | |
60 | static_assert( check_readable_traits<D, none> ); | |
61 | static_assert( check_readable_traits<const D, none> ); | |
62 | ||
63 | struct E { }; | |
64 | template<> | |
65 | struct std::readable_traits<E> { using value_type = long; }; | |
66 | static_assert( check_readable_traits<E, long> ); | |
67 | static_assert( check_readable_traits<const E, long> ); | |
68 | ||
69 | template<typename T> | |
70 | concept has_alias = requires { typename std::iter_value_t<T>; }; | |
71 | ||
72 | // Check std::iter_value_t<T> is U (or doesn't exist). | |
73 | template<typename T, typename U> | |
74 | concept check_alias = (has_alias<T> != std::same_as<U, none>); | |
75 | ||
76 | static_assert( check_alias<void, none> ); | |
77 | static_assert( check_alias<const void, none> ); | |
78 | static_assert( check_alias<void*, none> ); | |
79 | static_assert( check_alias<const void*, none> ); | |
80 | ||
81 | static_assert( check_alias<int, none> ); | |
82 | static_assert( check_alias<const int, none> ); | |
83 | static_assert( check_alias<int*, std::ptrdiff_t> ); | |
84 | static_assert( check_alias<const int*, std::ptrdiff_t> ); | |
85 | static_assert( check_alias<int[2], std::ptrdiff_t> ); | |
86 | static_assert( check_alias<const int[2], std::ptrdiff_t> ); | |
87 | ||
88 | static_assert( check_alias<A, int> ); | |
89 | static_assert( check_alias<const A, int> ); | |
90 | static_assert( check_alias<B, none> ); | |
91 | static_assert( check_alias<C, none> ); | |
92 | static_assert( check_alias<const C, none> ); | |
93 | static_assert( check_alias<D, none> ); | |
94 | static_assert( check_alias<const D, none> ); | |
95 | static_assert( check_alias<E, long> ); | |
96 | static_assert( check_alias<const E, long> ); | |
97 | ||
98 | struct F { }; | |
99 | template<> | |
100 | struct std::iterator_traits<F> { using value_type = F; }; | |
101 | // iterator_traits<F> is specialized, so use its value_type. | |
102 | static_assert( check_alias<F, std::iterator_traits<F>::value_type> ); | |
103 | ||
104 | struct G { }; | |
105 | template<> | |
106 | struct std::readable_traits<G> { using value_type = G; }; | |
107 | template<> | |
108 | struct std::iterator_traits<G> { using value_type = int; }; | |
109 | // iterator_traits<G> is specialized, so use its value_type. | |
110 | static_assert( check_alias<G, std::iterator_traits<G>::value_type> ); | |
111 | ||
112 | struct H { }; | |
113 | template<> | |
114 | struct std::readable_traits<H> { using value_type = H; }; | |
115 | template<> | |
116 | struct std::iterator_traits<H> | |
117 | { | |
118 | using iterator_category = input_iterator_tag; | |
119 | using difference_type = int; | |
120 | using value_type = char; | |
121 | using reference = value_type&; | |
122 | }; | |
123 | // iterator_traits<H> is specialized, so use its value_type. | |
124 | static_assert( check_alias<H, std::iterator_traits<H>::value_type> ); | |
125 | ||
126 | struct I | |
127 | { | |
128 | using value_type = I; | |
129 | }; | |
130 | // iterator_traits<I> is not specialized, and no standard specialization | |
131 | // matches, so use readable_traits. | |
132 | static_assert( check_alias<I, std::readable_traits<I>::value_type> ); | |
133 | ||
134 | struct J | |
135 | { | |
136 | using iterator_category = std::input_iterator_tag; | |
137 | using difference_type = int; | |
138 | using value_type = char; | |
139 | using reference = value_type&; | |
140 | }; | |
141 | // iterator_traits<J> matches constrained specialization in the library, | |
142 | // so use its value_type. | |
143 | static_assert( check_alias<J, int> ); |