in_place_t.pass.cpp revision 01afa5c6e407e985d9643707d7b7ab1384bd9317
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// template <class... Args>
13//   constexpr explicit optional(in_place_t, Args&&... args);
14
15#include <optional>
16#include <type_traits>
17#include <cassert>
18
19#if _LIBCPP_STD_VER > 11
20
21class X
22{
23    int i_;
24    int j_ = 0;
25public:
26    X() : i_(0) {}
27    X(int i) : i_(i) {}
28    X(int i, int j) : i_(i), j_(j) {}
29
30    ~X() {}
31
32    friend bool operator==(const X& x, const X& y)
33        {return x.i_ == y.i_ && x.j_ == y.j_;}
34};
35
36class Y
37{
38    int i_;
39    int j_ = 0;
40public:
41    constexpr Y() : i_(0) {}
42    constexpr Y(int i) : i_(i) {}
43    constexpr Y(int i, int j) : i_(i), j_(j) {}
44
45    friend constexpr bool operator==(const Y& x, const Y& y)
46        {return x.i_ == y.i_ && x.j_ == y.j_;}
47};
48
49class Z
50{
51    int i_;
52public:
53    Z(int i) : i_(i) {throw 6;}
54};
55
56
57#endif  // _LIBCPP_STD_VER > 11
58
59int main()
60{
61#if _LIBCPP_STD_VER > 11
62    {
63        constexpr std::optional<int> opt(std::in_place, 5);
64        static_assert(static_cast<bool>(opt) == true, "");
65        static_assert(*opt == 5, "");
66
67        struct test_constexpr_ctor
68            : public std::optional<int>
69        {
70            constexpr test_constexpr_ctor(std::in_place_t, int i)
71                : std::optional<int>(std::in_place, i) {}
72        };
73
74    }
75    {
76        const std::optional<X> opt(std::in_place);
77        assert(static_cast<bool>(opt) == true);
78        assert(*opt == X());
79    }
80    {
81        const std::optional<X> opt(std::in_place, 5);
82        assert(static_cast<bool>(opt) == true);
83        assert(*opt == X(5));
84    }
85    {
86        const std::optional<X> opt(std::in_place, 5, 4);
87        assert(static_cast<bool>(opt) == true);
88        assert(*opt == X(5, 4));
89    }
90    {
91        constexpr std::optional<Y> opt(std::in_place);
92        static_assert(static_cast<bool>(opt) == true, "");
93        static_assert(*opt == Y(), "");
94
95        struct test_constexpr_ctor
96            : public std::optional<Y>
97        {
98            constexpr test_constexpr_ctor(std::in_place_t)
99                : std::optional<Y>(std::in_place) {}
100        };
101
102    }
103    {
104        constexpr std::optional<Y> opt(std::in_place, 5);
105        static_assert(static_cast<bool>(opt) == true, "");
106        static_assert(*opt == Y(5), "");
107
108        struct test_constexpr_ctor
109            : public std::optional<Y>
110        {
111            constexpr test_constexpr_ctor(std::in_place_t, int i)
112                : std::optional<Y>(std::in_place, i) {}
113        };
114
115    }
116    {
117        constexpr std::optional<Y> opt(std::in_place, 5, 4);
118        static_assert(static_cast<bool>(opt) == true, "");
119        static_assert(*opt == Y(5, 4), "");
120
121        struct test_constexpr_ctor
122            : public std::optional<Y>
123        {
124            constexpr test_constexpr_ctor(std::in_place_t, int i, int j)
125                : std::optional<Y>(std::in_place, i, j) {}
126        };
127
128    }
129    {
130        try
131        {
132            const std::optional<Z> opt(std::in_place, 1);
133            assert(false);
134        }
135        catch (int i)
136        {
137            assert(i == 6);
138        }
139    }
140#endif  // _LIBCPP_STD_VER > 11
141}
142