1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // UNSUPPORTED: c++98, c++03, c++11, c++14 10 11 // XFAIL: dylib-has-no-bad_optional_access && !no-exceptions 12 13 // <optional> 14 15 // constexpr optional(const T& v); 16 17 #include <optional> 18 #include <type_traits> 19 #include <cassert> 20 21 #include "test_macros.h" 22 #include "archetypes.h" 23 24 using std::optional; 25 26 int main(int, char**) 27 { 28 { 29 typedef int T; 30 constexpr T t(5); 31 constexpr optional<T> opt(t); 32 static_assert(static_cast<bool>(opt) == true, ""); 33 static_assert(*opt == 5, ""); 34 35 struct test_constexpr_ctor 36 : public optional<T> 37 { 38 constexpr test_constexpr_ctor(const T&) {} 39 }; 40 41 } 42 { 43 typedef double T; 44 constexpr T t(3); 45 constexpr optional<T> opt(t); 46 static_assert(static_cast<bool>(opt) == true, ""); 47 static_assert(*opt == 3, ""); 48 49 struct test_constexpr_ctor 50 : public optional<T> 51 { 52 constexpr test_constexpr_ctor(const T&) {} 53 }; 54 55 } 56 { 57 const int x = 42; 58 optional<const int> o(x); 59 assert(*o == x); 60 } 61 { 62 typedef TestTypes::TestType T; 63 T::reset(); 64 const T t(3); 65 optional<T> opt = t; 66 assert(T::alive == 2); 67 assert(T::copy_constructed == 1); 68 assert(static_cast<bool>(opt) == true); 69 assert(opt.value().value == 3); 70 } 71 { 72 typedef ExplicitTestTypes::TestType T; 73 static_assert(!std::is_convertible<T const&, optional<T>>::value, ""); 74 T::reset(); 75 const T t(3); 76 optional<T> opt(t); 77 assert(T::alive == 2); 78 assert(T::copy_constructed == 1); 79 assert(static_cast<bool>(opt) == true); 80 assert(opt.value().value == 3); 81 } 82 { 83 typedef ConstexprTestTypes::TestType T; 84 constexpr T t(3); 85 constexpr optional<T> opt = {t}; 86 static_assert(static_cast<bool>(opt) == true, ""); 87 static_assert(opt.value().value == 3, ""); 88 89 struct test_constexpr_ctor 90 : public optional<T> 91 { 92 constexpr test_constexpr_ctor(const T&) {} 93 }; 94 } 95 { 96 typedef ExplicitConstexprTestTypes::TestType T; 97 static_assert(!std::is_convertible<const T&, optional<T>>::value, ""); 98 constexpr T t(3); 99 constexpr optional<T> opt(t); 100 static_assert(static_cast<bool>(opt) == true, ""); 101 static_assert(opt.value().value == 3, ""); 102 103 struct test_constexpr_ctor 104 : public optional<T> 105 { 106 constexpr test_constexpr_ctor(const T&) {} 107 }; 108 109 } 110 #ifndef TEST_HAS_NO_EXCEPTIONS 111 { 112 struct Z { 113 Z(int) {} 114 Z(const Z&) {throw 6;} 115 }; 116 typedef Z T; 117 try 118 { 119 const T t(3); 120 optional<T> opt(t); 121 assert(false); 122 } 123 catch (int i) 124 { 125 assert(i == 6); 126 } 127 } 128 #endif 129 130 return 0; 131 } 132