From 09dfc35e79dc556e30b24ddca89c4d131e3d58cd Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Fri, 31 May 2024 10:51:57 +0800 Subject: [PATCH] fix optional --- src/sled/optional.h | 882 ++++++++++++++------------------------------ 1 file changed, 283 insertions(+), 599 deletions(-) diff --git a/src/sled/optional.h b/src/sled/optional.h index 0066508..1419fed 100644 --- a/src/sled/optional.h +++ b/src/sled/optional.h @@ -14,9 +14,8 @@ // . /// -#pragma once -#ifndef SLED_TL_OPTIONAL_HPP -#define SLED_TL_OPTIONAL_HPP +#ifndef TL_OPTIONAL_HPP +#define TL_OPTIONAL_HPP #define TL_OPTIONAL_VERSION_MAJOR 1 #define TL_OPTIONAL_VERSION_MINOR 1 @@ -32,38 +31,31 @@ #define TL_OPTIONAL_MSVC2015 #endif -#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 \ - && !defined(__clang__)) +#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__)) #define TL_OPTIONAL_GCC49 #endif -#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 \ - && !defined(__clang__)) +#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 4 && !defined(__clang__)) #define TL_OPTIONAL_GCC54 #endif -#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 \ - && !defined(__clang__)) +#if (defined(__GNUC__) && __GNUC__ == 5 && __GNUC_MINOR__ <= 5 && !defined(__clang__)) #define TL_OPTIONAL_GCC55 #endif -#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 \ - && !defined(__clang__)) +#if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ <= 9 && !defined(__clang__)) // GCC < 5 doesn't support overloading on const&& for member functions #define TL_OPTIONAL_NO_CONSTRR // GCC < 5 doesn't support some standard C++11 type traits -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ - std::has_trivial_copy_constructor::value -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ - std::has_trivial_copy_assign::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::has_trivial_copy_constructor::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::has_trivial_copy_assign::value // This one will be different for GCC 5.7 if it's ever supported -#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \ - std::is_trivially_destructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value -// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks std::vector -// for non-copyable types +// GCC 5 < v < 8 has a bug in is_trivially_copy_constructible which breaks +// std::vector for non-copyable types #elif (defined(__GNUC__) && __GNUC__ < 8 && !defined(__clang__)) #ifndef TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX #define TL_GCC_LESS_8_TRIVIALLY_COPY_CONSTRUCTIBLE_MUTEX @@ -71,30 +63,22 @@ namespace tl { namespace detail { template -struct is_trivially_copy_constructible - : std::is_trivially_copy_constructible {}; +struct is_trivially_copy_constructible : std::is_trivially_copy_constructible {}; #ifdef _GLIBCXX_VECTOR template -struct is_trivially_copy_constructible> - : std::is_trivially_copy_constructible {}; +struct is_trivially_copy_constructible> : std::is_trivially_copy_constructible {}; #endif }// namespace detail }// namespace tl #endif -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ - tl::detail::is_trivially_copy_constructible::value -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ - std::is_trivially_copy_assignable::value -#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \ - std::is_trivially_destructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) tl::detail::is_trivially_copy_constructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value #else -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) \ - std::is_trivially_copy_constructible::value -#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) \ - std::is_trivially_copy_assignable::value -#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) \ - std::is_trivially_destructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) std::is_trivially_copy_constructible::value +#define TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) std::is_trivially_copy_assignable::value +#define TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T) std::is_trivially_destructible::value #endif #if __cplusplus > 201103L @@ -102,8 +86,7 @@ struct is_trivially_copy_constructible> #endif // constexpr implies const in C++11, not C++14 -#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) \ - || defined(TL_OPTIONAL_GCC49)) +#if (__cplusplus == 201103L || defined(TL_OPTIONAL_MSVC2015) || defined(TL_OPTIONAL_GCC49)) #define TL_OPTIONAL_11_CONSTEXPR #else #define TL_OPTIONAL_11_CONSTEXPR constexpr @@ -151,8 +134,7 @@ template struct conjunction : B {}; template -struct conjunction - : std::conditional, B>::type {}; +struct conjunction : std::conditional, B>::type {}; #if defined(_LIBCPP_VERSION) && __cplusplus == 201103L #define TL_TRAITS_LIBCXX_MEM_FN_WORKAROUND @@ -166,28 +148,22 @@ template struct is_pointer_to_non_const_member_func : std::false_type {}; template -struct is_pointer_to_non_const_member_func - : std::true_type {}; +struct is_pointer_to_non_const_member_func : std::true_type {}; template -struct is_pointer_to_non_const_member_func - : std::true_type {}; +struct is_pointer_to_non_const_member_func : std::true_type {}; template -struct is_pointer_to_non_const_member_func - : std::true_type {}; +struct is_pointer_to_non_const_member_func : std::true_type {}; template -struct is_pointer_to_non_const_member_func - : std::true_type {}; +struct is_pointer_to_non_const_member_func : std::true_type {}; template -struct is_pointer_to_non_const_member_func - : std::true_type {}; +struct is_pointer_to_non_const_member_func : std::true_type {}; template -struct is_pointer_to_non_const_member_func - : std::true_type {}; +struct is_pointer_to_non_const_member_func : std::true_type {}; template struct is_const_or_const_ref : std::false_type {}; @@ -204,25 +180,21 @@ struct is_const_or_const_ref : std::true_type {}; template::value - && is_const_or_const_ref::value)>, + typename + = enable_if_t::value && is_const_or_const_ref::value)>, #endif typename = enable_if_t>::value>, - int = 0> + int = 0> constexpr auto -invoke(Fn &&f, Args &&...args) noexcept( - noexcept(std::mem_fn(f)(std::forward(args)...))) +invoke(Fn &&f, Args &&...args) noexcept(noexcept(std::mem_fn(f)(std::forward(args)...))) -> decltype(std::mem_fn(f)(std::forward(args)...)) { return std::mem_fn(f)(std::forward(args)...); } -template>::value>> +template>::value>> constexpr auto -invoke(Fn &&f, Args &&...args) noexcept( - noexcept(std::forward(f)(std::forward(args)...))) +invoke(Fn &&f, Args &&...args) noexcept(noexcept(std::forward(f)(std::forward(args)...))) -> decltype(std::forward(f)(std::forward(args)...)) { return std::forward(f)(std::forward(args)...); @@ -233,12 +205,8 @@ template struct invoke_result_impl; template -struct invoke_result_impl< - F, - decltype(detail::invoke(std::declval(), std::declval()...), void()), - Us...> { - using type = - decltype(detail::invoke(std::declval(), std::declval()...)); +struct invoke_result_impl(), std::declval()...), void()), Us...> { + using type = decltype(detail::invoke(std::declval(), std::declval()...)); }; template @@ -270,62 +238,50 @@ tag swap(T (&a)[N], T (&b)[N]); // becomes std::swap template std::false_type can_swap(...) noexcept(false); -template(), std::declval()))> -std::true_type can_swap(int) noexcept(noexcept(swap(std::declval(), - std::declval()))); +template(), std::declval()))> +std::true_type can_swap(int) noexcept(noexcept(swap(std::declval(), std::declval()))); template std::false_type uses_std(...); template -std::is_same(), std::declval())), tag> -uses_std(int); +std::is_same(), std::declval())), tag> uses_std(int); template struct is_std_swap_noexcept : std::integral_constant::value - && std::is_nothrow_move_assignable::value> { -}; + && std::is_nothrow_move_assignable::value> {}; template struct is_std_swap_noexcept : is_std_swap_noexcept {}; template -struct is_adl_swap_noexcept - : std::integral_constant(0))> {}; +struct is_adl_swap_noexcept : std::integral_constant(0))> {}; }// namespace swap_adl_tests template struct is_swappable - : std::integral_constant< - bool, - decltype(detail::swap_adl_tests::can_swap(0))::value - && (!decltype(detail::swap_adl_tests::uses_std(0))::value - || (std::is_move_assignable::value - && std::is_move_constructible::value))> {}; + : std::integral_constant(0))::value + && (!decltype(detail::swap_adl_tests::uses_std(0))::value + || (std::is_move_assignable::value && std::is_move_constructible::value))> { +}; template struct is_swappable - : std::integral_constant< - bool, - decltype(detail::swap_adl_tests::can_swap(0))::value - && (!decltype(detail::swap_adl_tests::uses_std( - 0))::value - || is_swappable::value)> {}; + : std::integral_constant(0))::value + && (!decltype(detail::swap_adl_tests::uses_std(0))::value + || is_swappable::value)> {}; template struct is_nothrow_swappable - : std::integral_constant< - bool, - is_swappable::value - && ((decltype(detail::swap_adl_tests::uses_std(0))::value - && detail::swap_adl_tests::is_std_swap_noexcept::value) - || (!decltype(detail::swap_adl_tests::uses_std( - 0))::value - && detail::swap_adl_tests::is_adl_swap_noexcept:: - value))> {}; + : std::integral_constant::value + && ((decltype(detail::swap_adl_tests::uses_std(0))::value + && detail::swap_adl_tests::is_std_swap_noexcept::value) + || (!decltype(detail::swap_adl_tests::uses_std(0))::value + && detail::swap_adl_tests::is_adl_swap_noexcept::value))> {}; #endif #endif @@ -358,8 +314,7 @@ template struct returns_void_impl; template -struct returns_void_impl>, U...> - : std::is_void> {}; +struct returns_void_impl>, U...> : std::is_void> {}; template using returns_void = returns_void_impl; @@ -370,56 +325,40 @@ template using disable_if_ret_void = enable_if_t::value>; template -using enable_forward_value = detail::enable_if_t< - std::is_constructible::value - && !std::is_same, in_place_t>::value - && !std::is_same, detail::decay_t>::value>; +using enable_forward_value + = detail::enable_if_t::value && !std::is_same, in_place_t>::value + && !std::is_same, detail::decay_t>::value>; template using enable_from_other = detail::enable_if_t< - std::is_constructible::value - && !std::is_constructible &>::value - && !std::is_constructible &&>::value - && !std::is_constructible &>::value - && !std::is_constructible &&>::value - && !std::is_convertible &, T>::value - && !std::is_convertible &&, T>::value - && !std::is_convertible &, T>::value + std::is_constructible::value && !std::is_constructible &>::value + && !std::is_constructible &&>::value && !std::is_constructible &>::value + && !std::is_constructible &&>::value && !std::is_convertible &, T>::value + && !std::is_convertible &&, T>::value && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value>; template -using enable_assign_forward = detail::enable_if_t< - !std::is_same, detail::decay_t>::value - && !detail::conjunction, - std::is_same>>::value - && std::is_constructible::value && std::is_assignable::value>; +using enable_assign_forward + = detail::enable_if_t, detail::decay_t>::value + && !detail::conjunction, std::is_same>>::value + && std::is_constructible::value && std::is_assignable::value>; template using enable_assign_from_other = detail::enable_if_t< - std::is_constructible::value - && std::is_assignable::value - && !std::is_constructible &>::value - && !std::is_constructible &&>::value - && !std::is_constructible &>::value - && !std::is_constructible &&>::value - && !std::is_convertible &, T>::value - && !std::is_convertible &&, T>::value - && !std::is_convertible &, T>::value - && !std::is_convertible &&, T>::value - && !std::is_assignable &>::value - && !std::is_assignable &&>::value - && !std::is_assignable &>::value - && !std::is_assignable &&>::value>; + std::is_constructible::value && std::is_assignable::value + && !std::is_constructible &>::value && !std::is_constructible &&>::value + && !std::is_constructible &>::value && !std::is_constructible &&>::value + && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value + && !std::is_convertible &, T>::value && !std::is_convertible &&, T>::value + && !std::is_assignable &>::value && !std::is_assignable &&>::value + && !std::is_assignable &>::value && !std::is_assignable &&>::value>; // The storage base manages the actual storage, and correctly propagates // trivial destruction from T. This case is for when T is not trivially // destructible. template::value> struct optional_storage_base { - TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept - : m_dummy(), - m_has_value(false) - {} + TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {} template TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&...u) @@ -448,10 +387,7 @@ struct optional_storage_base { // This case is for when T is trivially destructible. template struct optional_storage_base { - TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept - : m_dummy(), - m_has_value(false) - {} + TL_OPTIONAL_11_CONSTEXPR optional_storage_base() noexcept : m_dummy(), m_has_value(false) {} template TL_OPTIONAL_11_CONSTEXPR optional_storage_base(in_place_t, U &&...u) @@ -533,8 +469,7 @@ struct optional_copy_base : optional_operations_base { optional_copy_base() = default; - optional_copy_base(const optional_copy_base &rhs) - : optional_operations_base() + optional_copy_base(const optional_copy_base &rhs) : optional_operations_base() { if (rhs.has_value()) { this->construct(rhs.get()); @@ -543,9 +478,9 @@ struct optional_copy_base : optional_operations_base { } } - optional_copy_base(optional_copy_base &&rhs) = default; + optional_copy_base(optional_copy_base &&rhs) = default; optional_copy_base &operator=(const optional_copy_base &rhs) = default; - optional_copy_base &operator=(optional_copy_base &&rhs) = default; + optional_copy_base &operator=(optional_copy_base &&rhs) = default; }; // This class manages conditionally having a trivial move constructor @@ -566,11 +501,10 @@ template struct optional_move_base : optional_copy_base { using optional_copy_base::optional_copy_base; - optional_move_base() = default; + optional_move_base() = default; optional_move_base(const optional_move_base &rhs) = default; - optional_move_base(optional_move_base &&rhs) noexcept( - std::is_nothrow_move_constructible::value) + optional_move_base(optional_move_base &&rhs) noexcept(std::is_nothrow_move_constructible::value) { if (rhs.has_value()) { this->construct(std::move(rhs.get())); @@ -580,14 +514,13 @@ struct optional_move_base : optional_copy_base { } optional_move_base &operator=(const optional_move_base &rhs) = default; - optional_move_base &operator=(optional_move_base &&rhs) = default; + optional_move_base &operator=(optional_move_base &&rhs) = default; }; // This class manages conditionally having a trivial copy assignment operator template + bool = TL_OPTIONAL_IS_TRIVIALLY_COPY_ASSIGNABLE(T) && TL_OPTIONAL_IS_TRIVIALLY_COPY_CONSTRUCTIBLE(T) + && TL_OPTIONAL_IS_TRIVIALLY_DESTRUCTIBLE(T)> struct optional_copy_assign_base : optional_move_base { using optional_move_base::optional_move_base; }; @@ -596,7 +529,7 @@ template struct optional_copy_assign_base : optional_move_base { using optional_move_base::optional_move_base; - optional_copy_assign_base() = default; + optional_copy_assign_base() = default; optional_copy_assign_base(const optional_copy_assign_base &rhs) = default; optional_copy_assign_base(optional_copy_assign_base &&rhs) = default; @@ -607,8 +540,7 @@ struct optional_copy_assign_base : optional_move_base { return *this; } - optional_copy_assign_base & - operator=(optional_copy_assign_base &&rhs) = default; + optional_copy_assign_base &operator=(optional_copy_assign_base &&rhs) = default; }; // This class manages conditionally having a trivial move assignment operator @@ -618,9 +550,8 @@ struct optional_copy_assign_base : optional_move_base { // move assignable #ifndef TL_OPTIONAL_GCC49 template::value - && std::is_trivially_move_constructible::value - && std::is_trivially_move_assignable::value> + bool = std::is_trivially_destructible::value && std::is_trivially_move_constructible::value + && std::is_trivially_move_assignable::value> struct optional_move_assign_base : optional_copy_assign_base { using optional_copy_assign_base::optional_copy_assign_base; }; @@ -633,18 +564,15 @@ template struct optional_move_assign_base : optional_copy_assign_base { using optional_copy_assign_base::optional_copy_assign_base; - optional_move_assign_base() = default; + optional_move_assign_base() = default; optional_move_assign_base(const optional_move_assign_base &rhs) = default; optional_move_assign_base(optional_move_assign_base &&rhs) = default; - optional_move_assign_base & - operator=(const optional_move_assign_base &rhs) = default; + optional_move_assign_base &operator=(const optional_move_assign_base &rhs) = default; - optional_move_assign_base & - operator=(optional_move_assign_base &&rhs) noexcept( - std::is_nothrow_move_constructible::value - && std::is_nothrow_move_assignable::value) + optional_move_assign_base &operator=(optional_move_assign_base &&rhs) noexcept( + std::is_nothrow_move_constructible::value && std::is_nothrow_move_assignable::value) { this->assign(std::move(rhs)); return *this; @@ -657,100 +585,78 @@ template::value, bool EnableMove = std::is_move_constructible::value> struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base &operator=(optional_delete_ctor_base &&) noexcept = default; }; template struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base &operator=(optional_delete_ctor_base &&) noexcept = default; }; template struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base &operator=(optional_delete_ctor_base &&) noexcept = default; }; template struct optional_delete_ctor_base { - optional_delete_ctor_base() = default; - optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; - optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; - optional_delete_ctor_base & - operator=(const optional_delete_ctor_base &) = default; - optional_delete_ctor_base & - operator=(optional_delete_ctor_base &&) noexcept = default; + optional_delete_ctor_base() = default; + optional_delete_ctor_base(const optional_delete_ctor_base &) = delete; + optional_delete_ctor_base(optional_delete_ctor_base &&) noexcept = delete; + optional_delete_ctor_base &operator=(const optional_delete_ctor_base &) = default; + optional_delete_ctor_base &operator=(optional_delete_ctor_base &&) noexcept = default; }; // optional_delete_assign_base will conditionally delete copy and move // constructors depending on whether T is copy/move constructible + assignable template::value - && std::is_copy_assignable::value), - bool EnableMove = (std::is_move_constructible::value - && std::is_move_assignable::value)> + bool EnableCopy = (std::is_copy_constructible::value && std::is_copy_assignable::value), + bool EnableMove = (std::is_move_constructible::value && std::is_move_assignable::value)> struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = default; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = default; + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = default; + optional_delete_assign_base &operator=(const optional_delete_assign_base &) = default; + optional_delete_assign_base &operator=(optional_delete_assign_base &&) noexcept = default; }; template struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = default; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = delete; + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = default; + optional_delete_assign_base &operator=(const optional_delete_assign_base &) = default; + optional_delete_assign_base &operator=(optional_delete_assign_base &&) noexcept = delete; }; template struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = delete; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = default; + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = default; + optional_delete_assign_base &operator=(const optional_delete_assign_base &) = delete; + optional_delete_assign_base &operator=(optional_delete_assign_base &&) noexcept = default; }; template struct optional_delete_assign_base { - optional_delete_assign_base() = default; - optional_delete_assign_base(const optional_delete_assign_base &) = default; - optional_delete_assign_base(optional_delete_assign_base &&) noexcept = - default; - optional_delete_assign_base & - operator=(const optional_delete_assign_base &) = delete; - optional_delete_assign_base & - operator=(optional_delete_assign_base &&) noexcept = delete; + optional_delete_assign_base() = default; + optional_delete_assign_base(const optional_delete_assign_base &) = default; + optional_delete_assign_base(optional_delete_assign_base &&) noexcept = default; + optional_delete_assign_base &operator=(const optional_delete_assign_base &) = delete; + optional_delete_assign_base &operator=(optional_delete_assign_base &&) noexcept = delete; }; }// namespace detail @@ -763,8 +669,7 @@ struct nullopt_t { }; /// Represents an empty optional -static constexpr nullopt_t nullopt{nullopt_t::do_not_use{}, - nullopt_t::do_not_use{}}; +static constexpr nullopt_t nullopt{nullopt_t::do_not_use{}, nullopt_t::do_not_use{}}; class bad_optional_access : public std::exception { public: @@ -785,8 +690,7 @@ class optional : private detail::optional_move_assign_base, private detail::optional_delete_assign_base { using base = detail::optional_move_assign_base; - static_assert(!std::is_same::value, - "instantiation of optional with in_place_t is ill-formed"); + static_assert(!std::is_same::value, "instantiation of optional with in_place_t is ill-formed"); static_assert(!std::is_same, nullopt_t>::value, "instantiation of optional with nullopt_t is ill-formed"); @@ -795,42 +699,35 @@ public: // types are not SFINAE-safe. This provides better support for things like // generic lambdas. C.f. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) \ - && !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && !defined(TL_OPTIONAL_GCC54) \ + && !defined(TL_OPTIONAL_GCC55) /// Carries out some operation which returns an optional on the stored /// object if there is one. template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : result(nullopt); } template constexpr auto and_then(F &&f) const & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } #ifndef TL_OPTIONAL_NO_CONSTRR @@ -838,12 +735,9 @@ public: constexpr auto and_then(F &&f) const && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : result(nullopt); } #endif #else @@ -853,34 +747,27 @@ public: TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } template TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : result(nullopt); } template constexpr detail::invoke_result_t and_then(F &&f) const & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } #ifndef TL_OPTIONAL_NO_CONSTRR @@ -888,18 +775,15 @@ public: constexpr detail::invoke_result_t and_then(F &&f) const && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : result(nullopt); } #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) \ - && !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && !defined(TL_OPTIONAL_GCC54) \ + && !defined(TL_OPTIONAL_GCC55) /// Carries out some operation on the stored object if there is one. template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & @@ -927,44 +811,35 @@ public: #else /// Carries out some operation on the stored object if there is one. template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl( - std::declval(), - std::declval())) - map(F &&f) & + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), std::declval())) map(F &&f) & { return optional_map_impl(*this, std::forward(f)); } template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl( - std::declval(), - std::declval())) + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), std::declval())) map(F &&f) && { return optional_map_impl(std::move(*this), std::forward(f)); } template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) - map(F &&f) const & + constexpr decltype(optional_map_impl(std::declval(), std::declval())) map(F &&f) const & { return optional_map_impl(*this, std::forward(f)); } #ifndef TL_OPTIONAL_NO_CONSTRR template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) - map(F &&f) const && + constexpr decltype(optional_map_impl(std::declval(), std::declval())) map(F &&f) const && { return optional_map_impl(std::move(*this), std::forward(f)); } #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) \ - && !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && !defined(TL_OPTIONAL_GCC54) \ + && !defined(TL_OPTIONAL_GCC55) /// Carries out some operation on the stored object if there is one. template TL_OPTIONAL_11_CONSTEXPR auto transform(F &&f) & @@ -992,26 +867,21 @@ public: #else /// Carries out some operation on the stored object if there is one. template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl( - std::declval(), - std::declval())) + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), std::declval())) transform(F &&f) & { return optional_map_impl(*this, std::forward(f)); } template - TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl( - std::declval(), - std::declval())) + TL_OPTIONAL_11_CONSTEXPR decltype(optional_map_impl(std::declval(), std::declval())) transform(F &&f) && { return optional_map_impl(std::move(*this), std::forward(f)); } template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) + constexpr decltype(optional_map_impl(std::declval(), std::declval())) transform(F &&f) const & { return optional_map_impl(*this, std::forward(f)); @@ -1019,8 +889,7 @@ public: #ifndef TL_OPTIONAL_NO_CONSTRR template - constexpr decltype(optional_map_impl(std::declval(), - std::declval())) + constexpr decltype(optional_map_impl(std::declval(), std::declval())) transform(F &&f) const && { return optional_map_impl(std::move(*this), std::forward(f)); @@ -1095,32 +964,26 @@ public: template U map_or(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u); } template U map_or(F &&f, U &&u) && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u); } template U map_or(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u); } #ifndef TL_OPTIONAL_NO_CONSTRR template U map_or(F &&f, U &&u) const && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u); } #endif @@ -1129,32 +992,26 @@ public: template detail::invoke_result_t map_or_else(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u)(); } template detail::invoke_result_t map_or_else(F &&f, U &&u) && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u)(); } template detail::invoke_result_t map_or_else(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u)(); } #ifndef TL_OPTIONAL_NO_CONSTRR template detail::invoke_result_t map_or_else(F &&f, U &&u) const && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u)(); } #endif @@ -1167,15 +1024,9 @@ public: } /// Returns `rhs` if `*this` is empty, otherwise the current value. - TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & - { - return has_value() ? *this : rhs; - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { return has_value() ? *this : rhs; } - constexpr optional disjunction(const optional &rhs) const & - { - return has_value() ? *this : rhs; - } + constexpr optional disjunction(const optional &rhs) const & { return has_value() ? *this : rhs; } TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { @@ -1183,21 +1034,12 @@ public: } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(const optional &rhs) const && - { - return has_value() ? std::move(*this) : rhs; - } + constexpr optional disjunction(const optional &rhs) const && { return has_value() ? std::move(*this) : rhs; } #endif - TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & - { - return has_value() ? *this : std::move(rhs); - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { return has_value() ? *this : std::move(rhs); } - constexpr optional disjunction(optional &&rhs) const & - { - return has_value() ? *this : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const & { return has_value() ? *this : std::move(rhs); } TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { @@ -1205,10 +1047,7 @@ public: } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(optional &&rhs) const && - { - return has_value() ? std::move(*this) : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const && { return has_value() ? std::move(*this) : std::move(rhs); } #endif /// Takes the value out of the optional, leaving it empty @@ -1240,19 +1079,14 @@ public: /// Constructs the stored value in-place using the given arguments. template - constexpr explicit optional( - detail::enable_if_t::value, - in_place_t>, - Args &&...args) + constexpr explicit optional(detail::enable_if_t::value, in_place_t>, + Args &&...args) : base(in_place, std::forward(args)...) {} template TL_OPTIONAL_11_CONSTEXPR explicit optional( - detail::enable_if_t &, - Args &&...>::value, - in_place_t>, + detail::enable_if_t &, Args &&...>::value, in_place_t>, std::initializer_list il, Args &&...args) { @@ -1260,53 +1094,47 @@ public: } /// Constructs the stored value with `u`. - template< - class U = T, - detail::enable_if_t::value> * = nullptr, - detail::enable_forward_value * = nullptr> + template::value> * = nullptr, + detail::enable_forward_value * = nullptr> constexpr optional(U &&u) : base(in_place, std::forward(u)) {} - template< - class U = T, - detail::enable_if_t::value> * = nullptr, - detail::enable_forward_value * = nullptr> + template::value> * = nullptr, + detail::enable_forward_value * = nullptr> constexpr explicit optional(U &&u) : base(in_place, std::forward(u)) {} /// Converting copy constructor. template * = nullptr, - detail::enable_if_t::value> * = - nullptr> + detail::enable_from_other * = nullptr, + detail::enable_if_t::value> * = nullptr> optional(const optional &rhs) { if (rhs.has_value()) { this->construct(*rhs); } } template * = nullptr, - detail::enable_if_t::value> * = - nullptr> + detail::enable_from_other * = nullptr, + detail::enable_if_t::value> * = nullptr> explicit optional(const optional &rhs) { if (rhs.has_value()) { this->construct(*rhs); } } /// Converting move constructor. - template< - class U, - detail::enable_from_other * = nullptr, - detail::enable_if_t::value> * = nullptr> + template * = nullptr, + detail::enable_if_t::value> * = nullptr> optional(optional &&rhs) { if (rhs.has_value()) { this->construct(std::move(*rhs)); } } - template< - class U, - detail::enable_from_other * = nullptr, - detail::enable_if_t::value> * = nullptr> + template * = nullptr, + detail::enable_if_t::value> * = nullptr> explicit optional(optional &&rhs) { if (rhs.has_value()) { this->construct(std::move(*rhs)); } @@ -1358,8 +1186,7 @@ public: /// /// Copies the value from `rhs` if there is one. Otherwise resets the stored /// value in `*this`. - template * = nullptr> + template * = nullptr> optional &operator=(const optional &rhs) { if (has_value()) { @@ -1405,8 +1232,7 @@ public: template T &emplace(Args &&...args) { - static_assert(std::is_constructible::value, - "T must be constructible with Args"); + static_assert(std::is_constructible::value, "T must be constructible with Args"); *this = nullopt; this->construct(std::forward(args)...); @@ -1414,9 +1240,7 @@ public: } template - detail::enable_if_t< - std::is_constructible &, Args &&...>::value, - T &> + detail::enable_if_t &, Args &&...>::value, T &> emplace(std::initializer_list il, Args &&...args) { *this = nullopt; @@ -1430,9 +1254,8 @@ public: /// If both have a value, the values are swapped. /// If one has a value, it is moved to the other and the movee is left /// valueless. - void - swap(optional &rhs) noexcept(std::is_nothrow_move_constructible::value - && detail::is_nothrow_swappable::value) + void swap(optional &rhs) noexcept(std::is_nothrow_move_constructible::value + && detail::is_nothrow_swappable::value) { using std::swap; if (has_value()) { @@ -1450,42 +1273,28 @@ public: } /// Returns a pointer to the stored value - constexpr const T *operator->() const - { - return std::addressof(this->m_value); - } + constexpr const T *operator->() const { return std::addressof(this->m_value); } - TL_OPTIONAL_11_CONSTEXPR T *operator->() - { - return std::addressof(this->m_value); - } + TL_OPTIONAL_11_CONSTEXPR T *operator->() { return std::addressof(this->m_value); } /// Returns the stored value TL_OPTIONAL_11_CONSTEXPR T &operator*() & { return this->m_value; } constexpr const T &operator*() const & { return this->m_value; } - TL_OPTIONAL_11_CONSTEXPR T &&operator*() && - { - return std::move(this->m_value); - } + TL_OPTIONAL_11_CONSTEXPR T &&operator*() && { return std::move(this->m_value); } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr const T &&operator*() const && - { - return std::move(this->m_value); - } + constexpr const T &&operator*() const && { return std::move(this->m_value); } #endif /// Returns whether or not the optional has a value constexpr bool has_value() const noexcept { return this->m_has_value; } - constexpr explicit operator bool() const noexcept - { - return this->m_has_value; - } + constexpr explicit operator bool() const noexcept { return this->m_has_value; } - /// Returns the contained value if there is one, otherwise throws bad_optional_access + /// Returns the contained value if there is one, otherwise throws + /// bad_optional_access TL_OPTIONAL_11_CONSTEXPR T &value() & { if (has_value()) return this->m_value; @@ -1516,8 +1325,7 @@ public: template constexpr T value_or(U &&u) const & { - static_assert(std::is_copy_constructible::value - && std::is_convertible::value, + static_assert(std::is_copy_constructible::value && std::is_convertible::value, "T must be copy constructible and convertible from U"); return has_value() ? **this : static_cast(std::forward(u)); } @@ -1525,11 +1333,9 @@ public: template TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && { - static_assert(std::is_move_constructible::value - && std::is_convertible::value, + static_assert(std::is_move_constructible::value && std::is_convertible::value, "T must be move constructible and convertible from U"); - return has_value() ? std::move(**this) - : static_cast(std::forward(u)); + return has_value() ? std::move(**this) : static_cast(std::forward(u)); } /// Destroys the stored value if one exists, making the optional empty @@ -1547,16 +1353,14 @@ template inline constexpr bool operator==(const optional &lhs, const optional &rhs) { - return lhs.has_value() == rhs.has_value() - && (!lhs.has_value() || *lhs == *rhs); + return lhs.has_value() == rhs.has_value() && (!lhs.has_value() || *lhs == *rhs); } template inline constexpr bool operator!=(const optional &lhs, const optional &rhs) { - return lhs.has_value() != rhs.has_value() - || (lhs.has_value() && *lhs != *rhs); + return lhs.has_value() != rhs.has_value() || (lhs.has_value() && *lhs != *rhs); } template @@ -1759,7 +1563,7 @@ operator>=(const U &lhs, const optional &rhs) template::value> * = nullptr, - detail::enable_if_t::value> * = nullptr> + detail::enable_if_t::value> * = nullptr> void swap(optional &lhs, optional &rhs) noexcept(noexcept(lhs.swap(rhs))) { @@ -1772,10 +1576,7 @@ struct i_am_secret {}; template::value, - detail::decay_t, - T>> + class Ret = detail::conditional_t::value, detail::decay_t, T>> inline constexpr optional make_optional(U &&v) { @@ -1806,21 +1607,17 @@ namespace detail { #ifdef TL_OPTIONAL_CXX14 template(), - *std::declval())), + class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto optional_map_impl(Opt &&opt, F &&f) { - return opt.has_value() - ? detail::invoke(std::forward(f), *std::forward(opt)) - : optional(nullopt); + return opt.has_value() ? detail::invoke(std::forward(f), *std::forward(opt)) : optional(nullopt); } template(), - *std::declval())), + class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> auto optional_map_impl(Opt &&opt, F &&f) @@ -1835,22 +1632,18 @@ optional_map_impl(Opt &&opt, F &&f) #else template(), - *std::declval())), + class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> constexpr auto optional_map_impl(Opt &&opt, F &&f) -> optional { - return opt.has_value() - ? detail::invoke(std::forward(f), *std::forward(opt)) - : optional(nullopt); + return opt.has_value() ? detail::invoke(std::forward(f), *std::forward(opt)) : optional(nullopt); } template(), - *std::declval())), + class Ret = decltype(detail::invoke(std::declval(), *std::declval())), detail::enable_if_t::value> * = nullptr> auto @@ -1875,8 +1668,8 @@ public: // types are not SFINAE-safe. This provides better support for things like // generic lambdas. C.f. // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0826r0.html -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) \ - && !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && !defined(TL_OPTIONAL_GCC54) \ + && !defined(TL_OPTIONAL_GCC55) /// Carries out some operation which returns an optional on the stored /// object if there is one. @@ -1884,33 +1677,27 @@ public: TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } template TL_OPTIONAL_11_CONSTEXPR auto and_then(F &&f) && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } template constexpr auto and_then(F &&f) const & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } #ifndef TL_OPTIONAL_NO_CONSTRR @@ -1918,11 +1705,9 @@ public: constexpr auto and_then(F &&f) const && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } #endif #else @@ -1932,33 +1717,27 @@ public: TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } template TL_OPTIONAL_11_CONSTEXPR detail::invoke_result_t and_then(F &&f) && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } template constexpr detail::invoke_result_t and_then(F &&f) const & { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() ? detail::invoke(std::forward(f), **this) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), **this) : result(nullopt); } #ifndef TL_OPTIONAL_NO_CONSTRR @@ -1966,18 +1745,15 @@ public: constexpr detail::invoke_result_t and_then(F &&f) const && { using result = detail::invoke_result_t; - static_assert(detail::is_optional::value, - "F must return an optional"); + static_assert(detail::is_optional::value, "F must return an optional"); - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : result(nullopt); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : result(nullopt); } #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) \ - && !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && !defined(TL_OPTIONAL_GCC54) \ + && !defined(TL_OPTIONAL_GCC55) /// Carries out some operation on the stored object if there is one. template TL_OPTIONAL_11_CONSTEXPR auto map(F &&f) & @@ -2005,27 +1781,21 @@ public: #else /// Carries out some operation on the stored object if there is one. template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), std::declval())) map(F &&f) & { return detail::optional_map_impl(*this, std::forward(f)); } template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), std::declval())) map(F &&f) && { return detail::optional_map_impl(std::move(*this), std::forward(f)); } template - constexpr decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + constexpr decltype(detail::optional_map_impl(std::declval(), std::declval())) map(F &&f) const & { return detail::optional_map_impl(*this, std::forward(f)); @@ -2033,9 +1803,7 @@ public: #ifndef TL_OPTIONAL_NO_CONSTRR template - constexpr decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + constexpr decltype(detail::optional_map_impl(std::declval(), std::declval())) map(F &&f) const && { return detail::optional_map_impl(std::move(*this), std::forward(f)); @@ -2043,8 +1811,8 @@ public: #endif #endif -#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) \ - && !defined(TL_OPTIONAL_GCC54) && !defined(TL_OPTIONAL_GCC55) +#if defined(TL_OPTIONAL_CXX14) && !defined(TL_OPTIONAL_GCC49) && !defined(TL_OPTIONAL_GCC54) \ + && !defined(TL_OPTIONAL_GCC55) /// Carries out some operation on the stored object if there is one. template TL_OPTIONAL_11_CONSTEXPR auto transform(F &&f) & @@ -2072,9 +1840,7 @@ public: #else /// Carries out some operation on the stored object if there is one. template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), std::declval())) transform(F &&f) & { return detail::optional_map_impl(*this, std::forward(f)); @@ -2083,18 +1849,14 @@ public: /// \group map /// \synopsis template auto transform(F &&f) &&; template - TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + TL_OPTIONAL_11_CONSTEXPR decltype(detail::optional_map_impl(std::declval(), std::declval())) transform(F &&f) && { return detail::optional_map_impl(std::move(*this), std::forward(f)); } template - constexpr decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + constexpr decltype(detail::optional_map_impl(std::declval(), std::declval())) transform(F &&f) const & { return detail::optional_map_impl(*this, std::forward(f)); @@ -2102,9 +1864,7 @@ public: #ifndef TL_OPTIONAL_NO_CONSTRR template - constexpr decltype(detail::optional_map_impl( - std::declval(), - std::declval())) + constexpr decltype(detail::optional_map_impl(std::declval(), std::declval())) transform(F &&f) const && { return detail::optional_map_impl(std::move(*this), std::forward(f)); @@ -2179,32 +1939,26 @@ public: template U map_or(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u); } template U map_or(F &&f, U &&u) && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u); } template U map_or(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u); } #ifndef TL_OPTIONAL_NO_CONSTRR template U map_or(F &&f, U &&u) const && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u); } #endif @@ -2213,32 +1967,26 @@ public: template detail::invoke_result_t map_or_else(F &&f, U &&u) & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u)(); } template detail::invoke_result_t map_or_else(F &&f, U &&u) && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u)(); } template detail::invoke_result_t map_or_else(F &&f, U &&u) const & { - return has_value() ? detail::invoke(std::forward(f), **this) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), **this) : std::forward(u)(); } #ifndef TL_OPTIONAL_NO_CONSTRR template detail::invoke_result_t map_or_else(F &&f, U &&u) const && { - return has_value() - ? detail::invoke(std::forward(f), std::move(**this)) - : std::forward(u)(); + return has_value() ? detail::invoke(std::forward(f), std::move(**this)) : std::forward(u)(); } #endif @@ -2251,15 +1999,9 @@ public: } /// Returns `rhs` if `*this` is empty, otherwise the current value. - TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & - { - return has_value() ? *this : rhs; - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) & { return has_value() ? *this : rhs; } - constexpr optional disjunction(const optional &rhs) const & - { - return has_value() ? *this : rhs; - } + constexpr optional disjunction(const optional &rhs) const & { return has_value() ? *this : rhs; } TL_OPTIONAL_11_CONSTEXPR optional disjunction(const optional &rhs) && { @@ -2267,21 +2009,12 @@ public: } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(const optional &rhs) const && - { - return has_value() ? std::move(*this) : rhs; - } + constexpr optional disjunction(const optional &rhs) const && { return has_value() ? std::move(*this) : rhs; } #endif - TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & - { - return has_value() ? *this : std::move(rhs); - } + TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) & { return has_value() ? *this : std::move(rhs); } - constexpr optional disjunction(optional &&rhs) const & - { - return has_value() ? *this : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const & { return has_value() ? *this : std::move(rhs); } TL_OPTIONAL_11_CONSTEXPR optional disjunction(optional &&rhs) && { @@ -2289,10 +2022,7 @@ public: } #ifndef TL_OPTIONAL_NO_CONSTRR - constexpr optional disjunction(optional &&rhs) const && - { - return has_value() ? std::move(*this) : std::move(rhs); - } + constexpr optional disjunction(optional &&rhs) const && { return has_value() ? std::move(*this) : std::move(rhs); } #endif /// Takes the value out of the optional, leaving it empty @@ -2323,18 +2053,14 @@ public: TL_OPTIONAL_11_CONSTEXPR optional(optional &&rhs) = default; /// Constructs the stored value with `u`. - template>::value> * = nullptr> + template>::value> * = nullptr> constexpr optional(U &&u) noexcept : m_value(std::addressof(u)) { - static_assert(std::is_lvalue_reference::value, - "U must be an lvalue"); + static_assert(std::is_lvalue_reference::value, "U must be an lvalue"); } template - constexpr explicit optional(const optional &rhs) noexcept - : optional(*rhs) + constexpr explicit optional(const optional &rhs) noexcept : optional(*rhs) {} /// No-op @@ -2356,13 +2082,10 @@ public: optional &operator=(const optional &rhs) = default; /// Rebinds this optional to `u`. - template>::value> * = nullptr> + template>::value> * = nullptr> optional &operator=(U &&u) { - static_assert(std::is_lvalue_reference::value, - "U must be an lvalue"); + static_assert(std::is_lvalue_reference::value, "U must be an lvalue"); m_value = std::addressof(u); return *this; } @@ -2379,9 +2102,7 @@ public: } /// Rebinds this optional to `u`. - template>::value> * = nullptr> + template>::value> * = nullptr> optional &emplace(U &&u) noexcept { return *this = std::forward(u); @@ -2401,12 +2122,10 @@ public: constexpr bool has_value() const noexcept { return m_value != nullptr; } - constexpr explicit operator bool() const noexcept - { - return m_value != nullptr; - } + constexpr explicit operator bool() const noexcept { return m_value != nullptr; } - /// Returns the contained value if there is one, otherwise throws bad_optional_access + /// Returns the contained value if there is one, otherwise throws + /// bad_optional_access TL_OPTIONAL_11_CONSTEXPR T &value() { if (has_value()) return *m_value; @@ -2423,8 +2142,7 @@ public: template constexpr T value_or(U &&u) const & noexcept { - static_assert(std::is_copy_constructible::value - && std::is_convertible::value, + static_assert(std::is_copy_constructible::value && std::is_convertible::value, "T must be copy constructible and convertible from U"); return has_value() ? **this : static_cast(std::forward(u)); } @@ -2433,8 +2151,7 @@ public: template TL_OPTIONAL_11_CONSTEXPR T value_or(U &&u) && noexcept { - static_assert(std::is_move_constructible::value - && std::is_convertible::value, + static_assert(std::is_move_constructible::value && std::is_convertible::value, "T must be move constructible and convertible from U"); return has_value() ? **this : static_cast(std::forward(u)); } @@ -2448,42 +2165,6 @@ private: }// namespace tl -namespace sled { - -using nullopt_t = tl::nullopt_t; -static constexpr nullopt_t nullopt{nullopt_t::do_not_use{}, - nullopt_t::do_not_use{}}; - -template -using optional = tl::optional; - -template::value, - tl::detail::decay_t, - T>> -inline constexpr optional -make_optional(U &&v) -{ - return optional(std::forward(v)); -} - -template -inline constexpr optional -make_optional(Args &&...args) -{ - return optional(tl::in_place, std::forward(args)...); -} - -template -inline constexpr optional -make_optional(std::initializer_list il, Args &&...args) -{ - return optional(tl::in_place, il, std::forward(args)...); -} -}// namespace sled - namespace std { // TODO SFINAE template @@ -2497,4 +2178,7 @@ struct hash> { }; }// namespace std -#endif // SLED_TL_OPTIONAL_HPP +namespace sled { +using namespace tl; +} +#endif