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