nullopt_t.pass.cpp revision 0cdbe6048173c1f05628dbc85430acf191a3e173
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// <optional>
11
12// optional<T>& operator=(nullopt_t) noexcept;
13
14#include <experimental/optional>
15#include <type_traits>
16#include <cassert>
17
18#if _LIBCPP_STD_VER > 11
19
20using std::experimental::optional;
21using std::experimental::nullopt_t;
22using std::experimental::nullopt;
23
24struct X
25{
26    static bool dtor_called;
27    ~X() {dtor_called = true;}
28};
29
30bool X::dtor_called = false;
31
32#endif  // _LIBCPP_STD_VER > 11
33
34int main()
35{
36#if _LIBCPP_STD_VER > 11
37    {
38        optional<int> opt;
39        static_assert(noexcept(opt = nullopt) == true, "");
40        opt = nullopt;
41        assert(static_cast<bool>(opt) == false);
42    }
43    {
44        optional<int> opt(3);
45        opt = nullopt;
46        assert(static_cast<bool>(opt) == false);
47    }
48    {
49        optional<X> opt;
50        static_assert(noexcept(opt = nullopt) == true, "");
51        assert(X::dtor_called == false);
52        opt = nullopt;
53        assert(X::dtor_called == false);
54        assert(static_cast<bool>(opt) == false);
55    }
56    {
57        X x;
58        {
59            optional<X> opt(x);
60            assert(X::dtor_called == false);
61            opt = nullopt;
62            assert(X::dtor_called == true);
63            assert(static_cast<bool>(opt) == false);
64        }
65    }
66#endif  // _LIBCPP_STD_VER > 11
67}
68