Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / utilities / optional / optional.object / optional.object.mod / reset.pass.cpp
blob112ee213b42a68d9b2ca6d327fdd5d1fe612831e
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
11 // <optional>
13 // void reset() noexcept;
15 #include <optional>
16 #include <type_traits>
17 #include <cassert>
19 #include "test_macros.h"
21 using std::optional;
23 struct X
25 static bool dtor_called;
26 X() = default;
27 X(const X&) = default;
28 X& operator=(const X&) = default;
29 ~X() {dtor_called = true;}
32 bool X::dtor_called = false;
34 constexpr bool check_reset()
37 optional<int> opt;
38 static_assert(noexcept(opt.reset()) == true, "");
39 opt.reset();
40 assert(static_cast<bool>(opt) == false);
43 optional<int> opt(3);
44 opt.reset();
45 assert(static_cast<bool>(opt) == false);
47 return true;
50 int main(int, char**)
52 check_reset();
53 #if TEST_STD_VER >= 20
54 static_assert(check_reset());
55 #endif
57 optional<X> opt;
58 static_assert(noexcept(opt.reset()) == true, "");
59 assert(X::dtor_called == false);
60 opt.reset();
61 assert(X::dtor_called == false);
62 assert(static_cast<bool>(opt) == false);
65 optional<X> opt(X{});
66 X::dtor_called = false;
67 opt.reset();
68 assert(X::dtor_called == true);
69 assert(static_cast<bool>(opt) == false);
70 X::dtor_called = false;
73 return 0;