is_nothrow_invocable.pass.cpp
8.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// type_traits
// is_nothrow_invocable
#include <type_traits>
#include <functional>
#include <vector>
#include "test_macros.h"
struct Tag {};
struct Implicit {
Implicit(int) noexcept {}
};
struct ThrowsImplicit {
ThrowsImplicit(int) {}
};
struct Explicit {
explicit Explicit(int) noexcept {}
};
template <bool IsNoexcept, class Ret, class... Args>
struct CallObject {
Ret operator()(Args&&...) const noexcept(IsNoexcept);
};
struct Sink {
template <class... Args>
void operator()(Args&&...) const noexcept {}
};
template <class Fn, class... Args>
constexpr bool throws_invocable() {
return std::is_invocable<Fn, Args...>::value &&
!std::is_nothrow_invocable<Fn, Args...>::value;
}
template <class Ret, class Fn, class... Args>
constexpr bool throws_invocable_r() {
return std::is_invocable_r<Ret, Fn, Args...>::value &&
!std::is_nothrow_invocable_r<Ret, Fn, Args...>::value;
}
// FIXME(EricWF) Don't test the where noexcept is *not* part of the type system
// once implementations have caught up.
void test_noexcept_function_pointers() {
struct Dummy {
void foo() noexcept {}
static void bar() noexcept {}
};
#if !defined(__cpp_noexcept_function_type)
{
// Check that PMF's and function pointers *work*. is_nothrow_invocable will always
// return false because 'noexcept' is not part of the function type.
static_assert(throws_invocable<decltype(&Dummy::foo), Dummy&>(), "");
static_assert(throws_invocable<decltype(&Dummy::bar)>(), "");
}
#else
{
// Check that PMF's and function pointers actually work and that
// is_nothrow_invocable returns true for noexcept PMF's and function
// pointers.
static_assert(
std::is_nothrow_invocable<decltype(&Dummy::foo), Dummy&>::value, "");
static_assert(std::is_nothrow_invocable<decltype(&Dummy::bar)>::value, "");
}
#endif
}
int main(int, char**) {
using AbominableFunc = void(...) const noexcept;
// Non-callable things
{
static_assert(!std::is_nothrow_invocable<void>::value, "");
static_assert(!std::is_nothrow_invocable<const void>::value, "");
static_assert(!std::is_nothrow_invocable<volatile void>::value, "");
static_assert(!std::is_nothrow_invocable<const volatile void>::value, "");
static_assert(!std::is_nothrow_invocable<std::nullptr_t>::value, "");
static_assert(!std::is_nothrow_invocable<int>::value, "");
static_assert(!std::is_nothrow_invocable<double>::value, "");
static_assert(!std::is_nothrow_invocable<int[]>::value, "");
static_assert(!std::is_nothrow_invocable<int[3]>::value, "");
static_assert(!std::is_nothrow_invocable<int*>::value, "");
static_assert(!std::is_nothrow_invocable<const int*>::value, "");
static_assert(!std::is_nothrow_invocable<int const*>::value, "");
static_assert(!std::is_nothrow_invocable<int&>::value, "");
static_assert(!std::is_nothrow_invocable<const int&>::value, "");
static_assert(!std::is_nothrow_invocable<int&&>::value, "");
static_assert(!std::is_nothrow_invocable<int, std::vector<int> >::value,
"");
static_assert(!std::is_nothrow_invocable<int, std::vector<int*> >::value,
"");
static_assert(!std::is_nothrow_invocable<int, std::vector<int**> >::value,
"");
static_assert(!std::is_nothrow_invocable<AbominableFunc>::value, "");
// with parameters
static_assert(!std::is_nothrow_invocable<int, int>::value, "");
static_assert(!std::is_nothrow_invocable<int, double, float>::value, "");
static_assert(!std::is_nothrow_invocable<int, char, float, double>::value,
"");
static_assert(!std::is_nothrow_invocable<Sink, AbominableFunc>::value, "");
static_assert(!std::is_nothrow_invocable<Sink, void>::value, "");
static_assert(!std::is_nothrow_invocable<Sink, const volatile void>::value,
"");
static_assert(!std::is_nothrow_invocable_r<int, void>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, const void>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, volatile void>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, const volatile void>::value,
"");
static_assert(!std::is_nothrow_invocable_r<int, std::nullptr_t>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, double>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int[]>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int[3]>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int*>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, const int*>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int const*>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int&>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, const int&>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int&&>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, std::vector<int> >::value,
"");
static_assert(!std::is_nothrow_invocable_r<int, std::vector<int*> >::value,
"");
static_assert(!std::is_nothrow_invocable_r<int, std::vector<int**> >::value,
"");
static_assert(!std::is_nothrow_invocable_r<void, AbominableFunc>::value,
"");
// with parameters
static_assert(!std::is_nothrow_invocable_r<int, int, int>::value, "");
static_assert(!std::is_nothrow_invocable_r<int, int, double, float>::value,
"");
static_assert(
!std::is_nothrow_invocable_r<int, int, char, float, double>::value, "");
static_assert(
!std::is_nothrow_invocable_r<void, Sink, AbominableFunc>::value, "");
static_assert(!std::is_nothrow_invocable_r<void, Sink, void>::value, "");
static_assert(
!std::is_nothrow_invocable_r<void, Sink, const volatile void>::value,
"");
}
{
// Check that the conversion to the return type is properly checked
using Fn = CallObject<true, int>;
static_assert(std::is_nothrow_invocable_r<Implicit, Fn>::value, "");
static_assert(std::is_nothrow_invocable_r<double, Fn>::value, "");
static_assert(std::is_nothrow_invocable_r<const volatile void, Fn>::value,
"");
static_assert(throws_invocable_r<ThrowsImplicit, Fn>(), "");
static_assert(!std::is_nothrow_invocable<Fn(), Explicit>(), "");
}
{
// Check that the conversion to the parameters is properly checked
using Fn = CallObject<true, void, const Implicit&, const ThrowsImplicit&>;
static_assert(
std::is_nothrow_invocable<Fn, Implicit&, ThrowsImplicit&>::value, "");
static_assert(std::is_nothrow_invocable<Fn, int, ThrowsImplicit&>::value,
"");
static_assert(throws_invocable<Fn, int, int>(), "");
static_assert(!std::is_nothrow_invocable<Fn>::value, "");
}
{
// Check that the noexcept-ness of function objects is checked.
using Fn = CallObject<true, void>;
using Fn2 = CallObject<false, void>;
static_assert(std::is_nothrow_invocable<Fn>::value, "");
static_assert(throws_invocable<Fn2>(), "");
}
{
// Check that PMD derefs are noexcept
using Fn = int(Tag::*);
static_assert(std::is_nothrow_invocable<Fn, Tag&>::value, "");
static_assert(std::is_nothrow_invocable_r<Implicit, Fn, Tag&>::value, "");
static_assert(throws_invocable_r<ThrowsImplicit, Fn, Tag&>(), "");
}
{
// Check for is_nothrow_invocable_v
using Fn = CallObject<true, int>;
static_assert(std::is_nothrow_invocable_v<Fn>, "");
static_assert(!std::is_nothrow_invocable_v<Fn, int>, "");
}
{
// Check for is_nothrow_invocable_r_v
using Fn = CallObject<true, int>;
static_assert(std::is_nothrow_invocable_r_v<void, Fn>, "");
static_assert(!std::is_nothrow_invocable_r_v<int, Fn, int>, "");
}
test_noexcept_function_pointers();
return 0;
}