| 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 | #ifndef _LIBCPP___OPTIONAL_MAKE_OPTIONAL_H |
| 10 | #define _LIBCPP___OPTIONAL_MAKE_OPTIONAL_H |
| 11 | |
| 12 | #include <__config> |
| 13 | #include <__fwd/optional.h> |
| 14 | #include <__type_traits/decay.h> |
| 15 | #include <__type_traits/enable_if.h> |
| 16 | #include <__type_traits/integral_constant.h> |
| 17 | #include <__type_traits/is_constructible.h> |
| 18 | #include <__type_traits/reference_constructs_from_temporary.h> |
| 19 | #include <__utility/forward.h> |
| 20 | #include <__utility/in_place.h> |
| 21 | |
| 22 | #include <initializer_list> |
| 23 | |
| 24 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 25 | # pragma GCC system_header |
| 26 | #endif |
| 27 | |
| 28 | _LIBCPP_PUSH_MACROS |
| 29 | #include <__undef_macros> |
| 30 | |
| 31 | #if _LIBCPP_STD_VER >= 17 |
| 32 | |
| 33 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 34 | |
| 35 | struct __make_optional_barrier_tag { |
| 36 | explicit __make_optional_barrier_tag() = default; |
| 37 | }; |
| 38 | |
| 39 | template <class _Tp, class... _Args> |
| 40 | inline constexpr bool __is_constructible_for_optional_v = is_constructible_v<_Tp, _Args...>; |
| 41 | |
| 42 | template <class _Tp, class... _Args> |
| 43 | struct __is_constructible_for_optional : bool_constant<__is_constructible_for_optional_v<_Tp, _Args...>> {}; |
| 44 | |
| 45 | template <class _Tp, class _Up, class... _Args> |
| 46 | inline constexpr bool __is_constructible_for_optional_initializer_list_v = |
| 47 | is_constructible_v<_Tp, initializer_list<_Up>&, _Args...>; |
| 48 | |
| 49 | # if _LIBCPP_STD_VER >= 26 |
| 50 | template <class _Tp, class... _Args> |
| 51 | inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Args...> = false; |
| 52 | |
| 53 | template <class _Tp, class _Arg> |
| 54 | inline constexpr bool __is_constructible_for_optional_v<_Tp&, _Arg> = |
| 55 | is_constructible_v<_Tp&, _Arg> && !reference_constructs_from_temporary_v<_Tp&, _Arg>; |
| 56 | |
| 57 | template <class _Tp, class _Up, class... _Args> |
| 58 | inline constexpr bool __is_constructible_for_optional_initializer_list_v<_Tp&, _Up, _Args...> = false; |
| 59 | # endif |
| 60 | |
| 61 | template < |
| 62 | # if _LIBCPP_STD_VER >= 26 |
| 63 | __make_optional_barrier_tag = __make_optional_barrier_tag{}, |
| 64 | # endif |
| 65 | class _Tp, |
| 66 | enable_if_t<is_constructible_v<decay_t<_Tp>, _Tp>, int> = 0> |
| 67 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) { |
| 68 | return optional<decay_t<_Tp>>(std::forward<_Tp>(__v)); |
| 69 | } |
| 70 | |
| 71 | template <class _Tp, class... _Args, enable_if_t<__is_constructible_for_optional_v<_Tp, _Args...>, int> = 0> |
| 72 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) { |
| 73 | return optional<_Tp>(in_place, std::forward<_Args>(__args)...); |
| 74 | } |
| 75 | |
| 76 | template <class _Tp, |
| 77 | class _Up, |
| 78 | class... _Args, |
| 79 | enable_if_t<__is_constructible_for_optional_initializer_list_v<_Tp, _Up, _Args...>, int> = 0> |
| 80 | [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> |
| 81 | make_optional(initializer_list<_Up> __il, _Args&&... __args) { |
| 82 | return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...); |
| 83 | } |
| 84 | |
| 85 | _LIBCPP_END_NAMESPACE_STD |
| 86 | |
| 87 | #endif // _LIBCPP_STD_VER >= 17 |
| 88 | |
| 89 | _LIBCPP_POP_MACROS |
| 90 | |
| 91 | #endif // _LIBCPP___OPTIONAL_MAKE_OPTIONAL_H |
| 92 | |