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