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