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