summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/nullopt_t.pass.cpp
blob: 7f39744f0557d632f385f40cffbe1f20d6e4ddcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//===----------------------------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

// <optional>

// optional<T>& operator=(nullopt_t) noexcept;

#include <experimental/optional>
#include <type_traits>
#include <cassert>

#if _LIBCPP_STD_VER > 11

using std::experimental::optional;
using std::experimental::nullopt_t;
using std::experimental::nullopt;

struct X
{
    static bool dtor_called;
    ~X() {dtor_called = true;}
};

bool X::dtor_called = false;

#endif  // _LIBCPP_STD_VER > 11

int main()
{
#if _LIBCPP_STD_VER > 11
    {
        optional<int> opt;
        static_assert(noexcept(opt = nullopt) == true, "");
        opt = nullopt;
        assert(static_cast<bool>(opt) == false);
    }
    {
        optional<int> opt(3);
        opt = nullopt;
        assert(static_cast<bool>(opt) == false);
    }
    {
        optional<X> opt;
        static_assert(noexcept(opt = nullopt) == true, "");
        assert(X::dtor_called == false);
        opt = nullopt;
        assert(X::dtor_called == false);
        assert(static_cast<bool>(opt) == false);
    }
    {
        X x;
        {
            optional<X> opt(x);
            assert(X::dtor_called == false);
            opt = nullopt;
            assert(X::dtor_called == true);
            assert(static_cast<bool>(opt) == false);
        }
    }
#endif  // _LIBCPP_STD_VER > 11
}
OpenPOWER on IntegriCloud