1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // <optional>
12 
13 // void reset() noexcept;
14 
15 #include <optional>
16 #include <type_traits>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 
21 using std::optional;
22 
23 struct X
24 {
25     static bool dtor_called;
~XX26     ~X() {dtor_called = true;}
27 };
28 
29 bool X::dtor_called = false;
30 
check_reset()31 constexpr bool check_reset()
32 {
33     {
34         optional<int> opt;
35         static_assert(noexcept(opt.reset()) == true, "");
36         opt.reset();
37         assert(static_cast<bool>(opt) == false);
38     }
39     {
40         optional<int> opt(3);
41         opt.reset();
42         assert(static_cast<bool>(opt) == false);
43     }
44     return true;
45 }
46 
main(int,char **)47 int main(int, char**)
48 {
49     check_reset();
50 #if TEST_STD_VER >= 20
51     static_assert(check_reset());
52 #endif
53     {
54         optional<X> opt;
55         static_assert(noexcept(opt.reset()) == true, "");
56         assert(X::dtor_called == false);
57         opt.reset();
58         assert(X::dtor_called == false);
59         assert(static_cast<bool>(opt) == false);
60     }
61     {
62         optional<X> opt(X{});
63         X::dtor_called = false;
64         opt.reset();
65         assert(X::dtor_called == true);
66         assert(static_cast<bool>(opt) == false);
67         X::dtor_called = false;
68     }
69 
70   return 0;
71 }
72