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
21using std::optional;
22
23struct X
24{
25 static bool dtor_called;
26 X() = default;
27 X(const X&) = default;
28 X& operator=(const X&) = default;
29 ~X() {dtor_called = true;}
30};
31
32bool X::dtor_called = false;
33
34constexpr bool check_reset()
35{
36 {
37 optional<int> opt;
38 static_assert(noexcept(opt.reset()) == true, "");
39 opt.reset();
40 assert(static_cast<bool>(opt) == false);
41 }
42 {
43 optional<int> opt(3);
44 opt.reset();
45 assert(static_cast<bool>(opt) == false);
46 }
47 return true;
48}
49
50int main(int, char**)
51{
52 check_reset();
53#if TEST_STD_VER >= 20
54 static_assert(check_reset());
55#endif
56 {
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);
63 }
64 {
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;
71 }
72
73 return 0;
74}
75

source code of libcxx/test/std/utilities/optional/optional.object/optional.object.mod/reset.pass.cpp