summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/experimental/optional/optional.object/optional.object.assign/assign_value.pass.cpp
blob: 0215417ce0b26636cd5629c4ad72e1fe79561db9 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
//===----------------------------------------------------------------------===//
//
//                     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.
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++98, c++03, c++11
// <optional>

// template <class U> optional<T>& operator=(U&& v);

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

using std::experimental::optional;

struct AllowConstAssign {
  AllowConstAssign() {}
  AllowConstAssign(AllowConstAssign const&) {}
  AllowConstAssign const& operator=(AllowConstAssign const&) const {
      return *this;
  }
};

struct X
{
};

int main()
{
    static_assert(std::is_assignable<optional<int>, int>::value, "");
    static_assert(std::is_assignable<optional<int>, int&>::value, "");
    static_assert(std::is_assignable<optional<int>&, int>::value, "");
    static_assert(std::is_assignable<optional<int>&, int&>::value, "");
    static_assert(std::is_assignable<optional<int>&, const int&>::value, "");
    static_assert(!std::is_assignable<const optional<int>&, const int&>::value, "");
    static_assert(!std::is_assignable<optional<int>, X>::value, "");
    {
        optional<int> opt;
        opt = 1;
        assert(static_cast<bool>(opt) == true);
        assert(*opt == 1);
    }
    {
        optional<int> opt;
        const int i = 2;
        opt = i;
        assert(static_cast<bool>(opt) == true);
        assert(*opt == i);
    }
    {
        optional<int> opt(3);
        const int i = 2;
        opt = i;
        assert(static_cast<bool>(opt) == true);
        assert(*opt == i);
    }
    {
        optional<const AllowConstAssign> opt;
        const AllowConstAssign other;
        opt = other;
    }
    {
        optional<std::unique_ptr<int>> opt;
        opt = std::unique_ptr<int>(new int(3));
        assert(static_cast<bool>(opt) == true);
        assert(**opt == 3);
    }
    {
        optional<std::unique_ptr<int>> opt(std::unique_ptr<int>(new int(2)));
        opt = std::unique_ptr<int>(new int(3));
        assert(static_cast<bool>(opt) == true);
        assert(**opt == 3);
    }
}
OpenPOWER on IntegriCloud