1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 // <optional>
12 
13 // optional<T>& operator=(nullopt_t) noexcept;
14 
15 #include <optional>
16 #include <type_traits>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "archetypes.hpp"
21 
22 using std::optional;
23 using std::nullopt_t;
24 using std::nullopt;
25 
main()26 int main()
27 {
28     {
29         optional<int> opt;
30         static_assert(noexcept(opt = nullopt) == true, "");
31         opt = nullopt;
32         assert(static_cast<bool>(opt) == false);
33     }
34     {
35         optional<int> opt(3);
36         opt = nullopt;
37         assert(static_cast<bool>(opt) == false);
38     }
39     using TT = TestTypes::TestType;
40     TT::reset();
41     {
42         optional<TT> opt;
43         static_assert(noexcept(opt = nullopt) == true, "");
44         assert(TT::destroyed == 0);
45         opt = nullopt;
46         assert(TT::constructed == 0);
47         assert(TT::alive == 0);
48         assert(TT::destroyed == 0);
49         assert(static_cast<bool>(opt) == false);
50     }
51     assert(TT::alive == 0);
52     assert(TT::destroyed == 0);
53     TT::reset();
54     {
55         optional<TT> opt(42);
56         assert(TT::destroyed == 0);
57         TT::reset_constructors();
58         opt = nullopt;
59         assert(TT::constructed == 0);
60         assert(TT::alive == 0);
61         assert(TT::destroyed == 1);
62         assert(static_cast<bool>(opt) == false);
63     }
64     assert(TT::alive == 0);
65     assert(TT::destroyed == 1);
66     TT::reset();
67 }
68