85 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 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 X24{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 34TEST_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 49int main(int, char**)50{51 check_reset();52#if TEST_STD_VER >= 2053 static_assert(check_reset());54#endif55 {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#if TEST_STD_VER >= 2673 {74 X x{};75 optional<X&> opt(x);76 X::dtor_called = false;77 opt.reset();78 assert(X::dtor_called == false);79 assert(static_cast<bool>(opt) == false);80 }81#endif82 83 return 0;84}85