| 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; |
| 26 | X() = default; |
| 27 | X(const X&) = default; |
| 28 | X& operator=(const X&) = default; |
| 29 | ~X() {dtor_called = true;} |
| 30 | }; |
| 31 | |
| 32 | bool X::dtor_called = false; |
| 33 | |
| 34 | TEST_CONSTEXPR_CXX20 bool check_reset() { |
| 35 | { |
| 36 | optional<int> opt; |
| 37 | static_assert(noexcept(opt.reset()) == true, "" ); |
| 38 | opt.reset(); |
| 39 | assert(static_cast<bool>(opt) == false); |
| 40 | } |
| 41 | { |
| 42 | optional<int> opt(3); |
| 43 | opt.reset(); |
| 44 | assert(static_cast<bool>(opt) == false); |
| 45 | } |
| 46 | return true; |
| 47 | } |
| 48 | |
| 49 | int main(int, char**) |
| 50 | { |
| 51 | check_reset(); |
| 52 | #if TEST_STD_VER >= 20 |
| 53 | static_assert(check_reset()); |
| 54 | #endif |
| 55 | { |
| 56 | optional<X> opt; |
| 57 | static_assert(noexcept(opt.reset()) == true, "" ); |
| 58 | assert(X::dtor_called == false); |
| 59 | opt.reset(); |
| 60 | assert(X::dtor_called == false); |
| 61 | assert(static_cast<bool>(opt) == false); |
| 62 | } |
| 63 | { |
| 64 | optional<X> opt(X{}); |
| 65 | X::dtor_called = false; |
| 66 | opt.reset(); |
| 67 | assert(X::dtor_called == true); |
| 68 | assert(static_cast<bool>(opt) == false); |
| 69 | X::dtor_called = false; |
| 70 | } |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |