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++98, 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 using std::optional; 20 21 struct X 22 { 23 static bool dtor_called; 24 ~X() {dtor_called = true;} 25 }; 26 27 bool X::dtor_called = false; 28 29 int main(int, char**) 30 { 31 { 32 optional<int> opt; 33 static_assert(noexcept(opt.reset()) == true, ""); 34 opt.reset(); 35 assert(static_cast<bool>(opt) == false); 36 } 37 { 38 optional<int> opt(3); 39 opt.reset(); 40 assert(static_cast<bool>(opt) == false); 41 } 42 { 43 optional<X> opt; 44 static_assert(noexcept(opt.reset()) == true, ""); 45 assert(X::dtor_called == false); 46 opt.reset(); 47 assert(X::dtor_called == false); 48 assert(static_cast<bool>(opt) == false); 49 } 50 { 51 optional<X> opt(X{}); 52 X::dtor_called = false; 53 opt.reset(); 54 assert(X::dtor_called == true); 55 assert(static_cast<bool>(opt) == false); 56 X::dtor_called = false; 57 } 58 59 return 0; 60 } 61