| 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 | // <optional> |
| 11 | |
| 12 | // constexpr optional() noexcept; |
| 13 | |
| 14 | #include <optional> |
| 15 | #include <type_traits> |
| 16 | #include <cassert> |
| 17 | |
| 18 | #include "test_macros.h" |
| 19 | #include "archetypes.h" |
| 20 | |
| 21 | using std::optional; |
| 22 | |
| 23 | template <class Opt> |
| 24 | void |
| 25 | test_constexpr() |
| 26 | { |
| 27 | static_assert(std::is_nothrow_default_constructible<Opt>::value, "" ); |
| 28 | static_assert(std::is_trivially_destructible<Opt>::value, "" ); |
| 29 | static_assert(std::is_trivially_destructible<typename Opt::value_type>::value, "" ); |
| 30 | |
| 31 | constexpr Opt opt; |
| 32 | static_assert(static_cast<bool>(opt) == false, "" ); |
| 33 | |
| 34 | struct test_constexpr_ctor |
| 35 | : public Opt |
| 36 | { |
| 37 | constexpr test_constexpr_ctor() {} |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | template <class Opt> |
| 42 | void |
| 43 | test() |
| 44 | { |
| 45 | static_assert(std::is_nothrow_default_constructible<Opt>::value, "" ); |
| 46 | static_assert(!std::is_trivially_destructible<Opt>::value, "" ); |
| 47 | static_assert(!std::is_trivially_destructible<typename Opt::value_type>::value, "" ); |
| 48 | { |
| 49 | Opt opt; |
| 50 | assert(static_cast<bool>(opt) == false); |
| 51 | } |
| 52 | { |
| 53 | const Opt opt; |
| 54 | assert(static_cast<bool>(opt) == false); |
| 55 | } |
| 56 | |
| 57 | struct test_constexpr_ctor |
| 58 | : public Opt |
| 59 | { |
| 60 | constexpr test_constexpr_ctor() {} |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | int main(int, char**) |
| 65 | { |
| 66 | test_constexpr<optional<int>>(); |
| 67 | test_constexpr<optional<int*>>(); |
| 68 | test_constexpr<optional<ImplicitTypes::NoCtors>>(); |
| 69 | test_constexpr<optional<NonTrivialTypes::NoCtors>>(); |
| 70 | test_constexpr<optional<NonConstexprTypes::NoCtors>>(); |
| 71 | test<optional<NonLiteralTypes::NoCtors>>(); |
| 72 | // EXTENSIONS |
| 73 | #if defined(_LIBCPP_VERSION) && 0 // FIXME these extensions are currently disabled. |
| 74 | test_constexpr<optional<int&>>(); |
| 75 | test_constexpr<optional<const int&>>(); |
| 76 | test_constexpr<optional<int&>>(); |
| 77 | test_constexpr<optional<NonLiteralTypes::NoCtors&>>(); |
| 78 | test_constexpr<optional<NonLiteralTypes::NoCtors&&>>(); |
| 79 | #endif |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |