rvalue_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// constexpr optional(T&& v);
13
14#include <optional>
15#include <type_traits>
16#include <cassert>
17
18#if _LIBCPP_STD_VER > 11
19
20class X
21{
22    int i_;
23public:
24    X(int i) : i_(i) {}
25    X(X&& x) : i_(x.i_) {}
26
27    friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;}
28};
29
30class Y
31{
32    int i_;
33public:
34    constexpr Y(int i) : i_(i) {}
35    constexpr Y(Y&& x) : i_(x.i_) {}
36
37    friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;}
38};
39
40class Z
41{
42    int i_;
43public:
44    Z(int i) : i_(i) {}
45    Z(Z&&) {throw 6;}
46};
47
48#endif  // _LIBCPP_STD_VER > 11
49
50int main()
51{
52#if _LIBCPP_STD_VER > 11
53    {
54        typedef int T;
55        constexpr std::optional<T> opt(T(5));
56        static_assert(static_cast<bool>(opt) == true, "");
57        static_assert(*opt == 5, "");
58
59        struct test_constexpr_ctor
60            : public std::optional<T>
61        {
62            constexpr test_constexpr_ctor(T&&) {}
63        };
64    }
65    {
66        typedef double T;
67        constexpr std::optional<T> opt(T(3));
68        static_assert(static_cast<bool>(opt) == true, "");
69        static_assert(*opt == 3, "");
70
71        struct test_constexpr_ctor
72            : public std::optional<T>
73        {
74            constexpr test_constexpr_ctor(T&&) {}
75        };
76    }
77    {
78        typedef X T;
79        std::optional<T> opt(T(3));
80        assert(static_cast<bool>(opt) == true);
81        assert(*opt == 3);
82    }
83    {
84        typedef Y T;
85        constexpr std::optional<T> opt(T(3));
86        static_assert(static_cast<bool>(opt) == true, "");
87        static_assert(*opt == 3, "");
88
89        struct test_constexpr_ctor
90            : public std::optional<T>
91        {
92            constexpr test_constexpr_ctor(T&&) {}
93        };
94    }
95    {
96        typedef Z T;
97        try
98        {
99            std::optional<T> opt(T(3));
100            assert(false);
101        }
102        catch (int i)
103        {
104            assert(i == 6);
105        }
106    }
107#endif  // _LIBCPP_STD_VER > 11
108}
109