]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/scopeguard/exit.cc
1 // { dg-do run { target c++20 } }
3 #include <experimental/scope>
4 #include <testsuite_hooks.h>
7 void funk() { ++da_funk
; }
11 ThrowingCopy() = default;
12 ThrowingCopy(ThrowingCopy
&&) noexcept(false) { VERIFY(false); }
13 ThrowingCopy(const ThrowingCopy
&) { if (nocopy
) throw 1; }
15 void operator()() const noexcept
{ ++counter
; }
17 static ThrowingCopy
create() noexcept
{ nocopy
= false; return {}; }
23 bool ThrowingCopy::nocopy
= false;
24 int ThrowingCopy::counter
= 0;
29 using std::experimental::scope_exit
;
32 auto d
= [&counter
] () { ++counter
; };
37 VERIFY( counter
== 1 );
47 VERIFY( counter
== 2 );
51 scope_exit
e2(std::move(e
));
53 VERIFY( counter
== 3 );
59 VERIFY( counter
== 3 );
70 VERIFY( counter
== 3 );
74 scope_exit
<void(&)()> e(funk
);
76 VERIFY( da_funk
== 1 );
78 static_assert(!std::is_move_assignable_v
<scope_exit
<void(*)()>>);
79 static_assert(!std::is_move_assignable_v
<scope_exit
<void(&)()>>);
80 static_assert(!std::is_move_assignable_v
<scope_exit
<ThrowingCopy
>>);
81 static_assert(!std::is_move_assignable_v
<scope_exit
<decltype(d
)>>);
84 ThrowingCopy::counter
= 0;
87 scope_exit
<ThrowingCopy
> e(ThrowingCopy::create());
88 ThrowingCopy::nocopy
= true;
89 scope_exit
<ThrowingCopy
> e2(std::move(e
));
95 VERIFY( ThrowingCopy::counter
== 1 );
97 scope_exit
<ThrowingCopy
> e(ThrowingCopy::create());
100 ThrowingCopy::nocopy
= true;
101 scope_exit
<ThrowingCopy
> e2(std::move(e
));
107 VERIFY( ThrowingCopy::counter
== 1 );
109 VERIFY( ThrowingCopy::counter
== 2 );
115 using std::experimental::scope_fail
;
118 auto d
= [&counter
] () { ++counter
; };
123 VERIFY( counter
== 0 );
133 VERIFY( counter
== 1 );
139 VERIFY( counter
== 1 );
144 scope_fail
f2(std::move(f
));
150 VERIFY( counter
== 2 );
161 VERIFY( counter
== 2 );
166 scope_fail
<void(&)()> e(funk
);
172 VERIFY( da_funk
== 1 );
174 static_assert(!std::is_move_assignable_v
<scope_fail
<void(*)()>>);
175 static_assert(!std::is_move_assignable_v
<scope_fail
<void(&)()>>);
176 static_assert(!std::is_move_assignable_v
<scope_fail
<ThrowingCopy
>>);
177 static_assert(!std::is_move_assignable_v
<scope_fail
<decltype(d
)>>);
180 ThrowingCopy::counter
= 0;
183 scope_fail
<ThrowingCopy
> f(ThrowingCopy::create());
184 ThrowingCopy::nocopy
= true;
185 scope_fail
<ThrowingCopy
> f2(std::move(f
));
191 VERIFY( ThrowingCopy::counter
== 1 );
193 scope_fail
<ThrowingCopy
> f(ThrowingCopy::create());
196 ThrowingCopy::nocopy
= true;
197 scope_fail
<ThrowingCopy
> f2(std::move(f
));
203 VERIFY( ThrowingCopy::counter
== 1 );
205 VERIFY( ThrowingCopy::counter
== 1 );
211 using std::experimental::scope_success
;
214 auto d
= [&counter
] () { ++counter
; };
219 VERIFY( counter
== 1 );
229 VERIFY( counter
== 1 );
233 scope_success
s2(std::move(s
));
235 VERIFY( counter
== 2 );
241 VERIFY( counter
== 2 );
252 VERIFY( counter
== 2 );
256 scope_success
<void(&)()> e(funk
);
258 VERIFY( da_funk
== 1 );
260 static_assert(!std::is_move_assignable_v
<scope_success
<void(*)()>>);
261 static_assert(!std::is_move_assignable_v
<scope_success
<void(&)()>>);
262 static_assert(!std::is_move_assignable_v
<scope_success
<ThrowingCopy
>>);
263 static_assert(!std::is_move_assignable_v
<scope_success
<decltype(d
)>>);
266 ThrowingCopy::counter
= 0;
269 scope_success
<ThrowingCopy
> s(ThrowingCopy::create());
270 ThrowingCopy::nocopy
= true;
271 scope_success
<ThrowingCopy
> s2(std::move(s
));
277 VERIFY( ThrowingCopy::counter
== 0 );
279 scope_success
<ThrowingCopy
> s(ThrowingCopy::create());
282 ThrowingCopy::nocopy
= true;
283 scope_success
<ThrowingCopy
> s2(std::move(s
));
289 VERIFY( ThrowingCopy::counter
== 0 );
291 VERIFY( ThrowingCopy::counter
== 1 );