]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/24_iterators/associated_types/readable.traits.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 24_iterators / associated_types / readable.traits.cc
CommitLineData
8d9254fc 1// Copyright (C) 2019-2020 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
23struct none;
24
25template<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).
30template<typename T, typename U>
31 concept check_readable_traits
32 = (has_readable_traits_type<T> != std::same_as<U, none>);
33
34static_assert( check_readable_traits<void, none> );
35static_assert( check_readable_traits<const void, none> );
36static_assert( check_readable_traits<void*, none> );
37static_assert( check_readable_traits<const void*, none> );
38
39static_assert( check_readable_traits<int, none> );
40static_assert( check_readable_traits<const int, none> );
41
42static_assert( check_readable_traits<int*, int> );
43static_assert( check_readable_traits<const int*, int> );
44static_assert( check_readable_traits<int[2], int> );
45static_assert( check_readable_traits<const int[2], int> );
46
47struct A { using value_type = int; };
48static_assert( check_readable_traits<A, int> );
49static_assert( check_readable_traits<const A, int> );
50struct B : private A { };
51static_assert( check_readable_traits<B, none> );
52
53struct C { };
54short operator-(C, C) { return 0; }
55static_assert( check_readable_traits<C, none> );
56static_assert( check_readable_traits<const C, none> );
57
58struct D { long operator*() const { return 1L; } };
59unsigned short operator-(D, D) { return 0; }
60static_assert( check_readable_traits<D, none> );
61static_assert( check_readable_traits<const D, none> );
62
63struct E { };
64template<>
65 struct std::readable_traits<E> { using value_type = long; };
66static_assert( check_readable_traits<E, long> );
67static_assert( check_readable_traits<const E, long> );
68
69template<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).
73template<typename T, typename U>
74 concept check_alias = (has_alias<T> != std::same_as<U, none>);
75
76static_assert( check_alias<void, none> );
77static_assert( check_alias<const void, none> );
78static_assert( check_alias<void*, none> );
79static_assert( check_alias<const void*, none> );
80
81static_assert( check_alias<int, none> );
82static_assert( check_alias<const int, none> );
83static_assert( check_alias<int*, std::ptrdiff_t> );
84static_assert( check_alias<const int*, std::ptrdiff_t> );
85static_assert( check_alias<int[2], std::ptrdiff_t> );
86static_assert( check_alias<const int[2], std::ptrdiff_t> );
87
88static_assert( check_alias<A, int> );
89static_assert( check_alias<const A, int> );
90static_assert( check_alias<B, none> );
91static_assert( check_alias<C, none> );
92static_assert( check_alias<const C, none> );
93static_assert( check_alias<D, none> );
94static_assert( check_alias<const D, none> );
95static_assert( check_alias<E, long> );
96static_assert( check_alias<const E, long> );
97
98struct F { };
99template<>
100 struct std::iterator_traits<F> { using value_type = F; };
101// iterator_traits<F> is specialized, so use its value_type.
102static_assert( check_alias<F, std::iterator_traits<F>::value_type> );
103
104struct G { };
105template<>
106 struct std::readable_traits<G> { using value_type = G; };
107template<>
108 struct std::iterator_traits<G> { using value_type = int; };
109// iterator_traits<G> is specialized, so use its value_type.
110static_assert( check_alias<G, std::iterator_traits<G>::value_type> );
111
112struct H { };
113template<>
114 struct std::readable_traits<H> { using value_type = H; };
115template<>
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.
124static_assert( check_alias<H, std::iterator_traits<H>::value_type> );
125
126struct 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.
132static_assert( check_alias<I, std::readable_traits<I>::value_type> );
133
134struct 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.
143static_assert( check_alias<J, int> );