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(const 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: 28 X(int i) : i_(i) {} 29 30 friend bool operator==(const X& x, const X& y) {return x.i_ == y.i_;} 31 }; 32 33 class Y 34 { 35 int i_; 36 public: 37 constexpr Y(int i) : i_(i) {} 38 39 friend constexpr bool operator==(const Y& x, const Y& y) {return x.i_ == y.i_;} 40 }; 41 42 class Z 43 { 44 public: 45 Z(int) {} 46 Z(const Z&) {TEST_THROW(6);} 47 }; 48 49 50 int main() 51 { 52 { 53 typedef int T; 54 constexpr T t(5); 55 constexpr optional<T> opt(t); 56 static_assert(static_cast<bool>(opt) == true, ""); 57 static_assert(*opt == 5, ""); 58 59 struct test_constexpr_ctor 60 : public optional<T> 61 { 62 constexpr test_constexpr_ctor(const T&) {} 63 }; 64 65 } 66 { 67 typedef double T; 68 constexpr T t(3); 69 constexpr optional<T> opt(t); 70 static_assert(static_cast<bool>(opt) == true, ""); 71 static_assert(*opt == 3, ""); 72 73 struct test_constexpr_ctor 74 : public optional<T> 75 { 76 constexpr test_constexpr_ctor(const T&) {} 77 }; 78 79 } 80 { 81 typedef X T; 82 const T t(3); 83 optional<T> opt(t); 84 assert(static_cast<bool>(opt) == true); 85 assert(*opt == 3); 86 } 87 { 88 typedef Y T; 89 constexpr T t(3); 90 constexpr optional<T> opt(t); 91 static_assert(static_cast<bool>(opt) == true, ""); 92 static_assert(*opt == 3, ""); 93 94 struct test_constexpr_ctor 95 : public optional<T> 96 { 97 constexpr test_constexpr_ctor(const T&) {} 98 }; 99 100 } 101 #ifndef TEST_HAS_NO_EXCEPTIONS 102 { 103 typedef Z T; 104 try 105 { 106 const T t(3); 107 optional<T> opt(t); 108 assert(false); 109 } 110 catch (int i) 111 { 112 assert(i == 6); 113 } 114 } 115 #endif 116 } 117