]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/is_nothrow_invocable/value.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / is_nothrow_invocable / value.cc
1 // Copyright (C) 2016-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++17" }
19 // { dg-do compile }
20
21 #include <type_traits>
22
23 #ifndef IS_NT_INVOCABLE_DEFINED
24 template<typename... T>
25 constexpr bool is_nt_invocable()
26 {
27 constexpr bool result = std::is_nothrow_invocable_v<T...>;
28 static_assert(std::is_nothrow_invocable<T...>::value == result);
29 return result;
30 }
31
32 template<typename R, typename... T>
33 constexpr bool is_nt_invocable_r()
34 {
35 constexpr bool result = std::is_nothrow_invocable_r_v<R, T...>;
36 static_assert(std::is_nothrow_invocable_r<R, T...>::value == result);
37 return result;
38 }
39 #endif
40
41 void test01()
42 {
43 struct T { T(int) { } };
44 struct NT { NT(int) noexcept { } };
45 struct Ex { explicit Ex(int) noexcept { } };
46
47 using func_type = void(*)();
48 static_assert( ! is_nt_invocable< func_type>(), "");
49
50 #if __cpp_noexcept_function_type
51 using func_type_nt = void(*)() noexcept;
52 static_assert( is_nt_invocable< func_type_nt >(), "");
53 #endif
54
55 struct X { };
56 using mem_type = int X::*;
57
58 static_assert( ! is_nt_invocable< mem_type >(), "");
59 static_assert( ! is_nt_invocable< mem_type, int >(), "");
60 static_assert( ! is_nt_invocable< mem_type, int& >(), "");
61
62 static_assert( is_nt_invocable< mem_type, X& >(), "");
63 static_assert( is_nt_invocable_r< int, mem_type, X& >(), "");
64 static_assert( is_nt_invocable_r< int&, mem_type, X& >(), "");
65 static_assert( is_nt_invocable_r< long, mem_type, X& >(), "");
66 static_assert( ! is_nt_invocable_r< long&, mem_type, X& >(),
67 "conversion fails, cannot bind long& to int");
68 static_assert( is_nt_invocable_r< int&, mem_type, X* >(), "");
69
70 static_assert( ! is_nt_invocable_r< T, mem_type, X& >(),
71 "conversion throws");
72 static_assert( is_nt_invocable_r< NT, mem_type, X& >(), "");
73 static_assert( ! is_nt_invocable_r< Ex, mem_type, X& >(),
74 "conversion fails, would use explicit constructor");
75
76 using memfun_type = int (X::*)();
77
78 static_assert( ! is_nt_invocable< memfun_type >(), "no object");
79 static_assert( ! is_nt_invocable< memfun_type, int >(), "no object");
80 static_assert( ! is_nt_invocable< memfun_type, int& >(), "no object");
81 static_assert( ! is_nt_invocable< memfun_type, X& >(), "call throws");
82 static_assert( ! is_nt_invocable< memfun_type, X* >(), "call throws");
83
84 static_assert( ! is_nt_invocable_r< T, memfun_type, X& >(), "call throws");
85 static_assert( ! is_nt_invocable_r< NT, memfun_type, X& >(), "call throws");
86 static_assert( ! is_nt_invocable_r< Ex, memfun_type, X& >(), "call throws");
87
88 #if __cpp_noexcept_function_type
89 using memfun_type_nt = int (X::*)() noexcept;
90
91 static_assert( ! is_nt_invocable< memfun_type_nt >(), "no object");
92 static_assert( ! is_nt_invocable< memfun_type_nt, int >(), "no object");
93 static_assert( ! is_nt_invocable< memfun_type_nt, int& >(), "no object");
94 static_assert( is_nt_invocable< memfun_type_nt, X& >(), "");
95 static_assert( is_nt_invocable< memfun_type_nt, X* >(), "");
96
97 static_assert( ! is_nt_invocable_r< T, memfun_type_nt, X& >(),
98 "conversion throws");
99 static_assert( is_nt_invocable_r< NT, memfun_type_nt, X& >(), "");
100 static_assert( ! is_nt_invocable_r< Ex, memfun_type_nt, X& >(),
101 "conversion fails, would use explicit constructor");
102 #endif
103
104 struct F {
105 int& operator()();
106 long& operator()() const noexcept;
107 short& operator()(int) &&;
108 char& operator()(int) const& noexcept;
109 private:
110 void operator()(int, int) noexcept;
111 };
112 using CF = const F;
113
114 static_assert( ! is_nt_invocable< F >(), "call throws");
115 static_assert( is_nt_invocable< CF >(), "");
116
117 static_assert( ! is_nt_invocable_r< int&, F >(), "call throws");
118 static_assert( is_nt_invocable_r< long&, CF >(), "");
119 static_assert( ! is_nt_invocable_r< T, F >(), "call throws");
120 static_assert( ! is_nt_invocable_r< NT, F >(), "call throws");
121 static_assert( ! is_nt_invocable_r< Ex, F >(), "call throws");
122 static_assert( ! is_nt_invocable_r< T, CF >(), "conversion throws");
123 static_assert( is_nt_invocable_r< NT, CF >(), "" );
124 static_assert( ! is_nt_invocable_r< Ex, CF >(), "conversion fails");
125
126 static_assert( ! is_nt_invocable< F, int >(), "call throws");
127 static_assert( is_nt_invocable< F&, int >(), "");
128
129 static_assert( ! is_nt_invocable_r< short&, F, int >(),
130 "call throws" );
131 static_assert( is_nt_invocable_r< char&, F&, int >(), "");
132 static_assert( ! is_nt_invocable_r< T, F&, int >(),
133 "conversion throws");
134 static_assert( is_nt_invocable_r< NT, F&, int >(), "");
135 static_assert( ! is_nt_invocable_r< Ex, F&, int >(),
136 "conversion fails, would use explicit constructor");
137
138 static_assert( is_nt_invocable< CF, int >(), "");
139 static_assert( is_nt_invocable< CF&, int >(), "");
140
141 static_assert( is_nt_invocable_r< char&, CF, int >(), "");
142 static_assert( is_nt_invocable_r< char&, CF&, int >(), "");
143
144 static_assert( ! is_nt_invocable_r< T, CF&, int >(),
145 "conversion throws");
146 static_assert( is_nt_invocable_r< NT, CF&, int >(), "");
147 static_assert( ! is_nt_invocable_r< Ex, CF&, int >(),
148 "conversion fails, would use explicit constructor");
149
150 static_assert( ! is_nt_invocable< F, int, int >(),
151 "would call private member");
152 static_assert( ! is_nt_invocable_r<void, F, int, int >(),
153 "would call private member");
154 }