43template <
typename T,
typename U>
44concept binds_without_temporary =
45 std::is_constructible_v<T&, U>
46#if defined(__cpp_lib_reference_from_temporary) && __cpp_lib_reference_from_temporary >= 202202L
47 && !std::reference_constructs_from_temporary_v<T&, U>;
49 && ( !std::is_const_v<T>
50 || (std::is_lvalue_reference_v<U> && std::is_convertible_v<std::remove_reference_t<U>*, T*>)
51 || ( std::is_class_v<std::remove_cvref_t<U>>
52 && !std::is_convertible_v<std::remove_reference_t<U>*, T*>
53 && !std::is_constructible_v<T&&, U>
71class optional_ref final
78 constexpr optional_ref() noexcept = default;
81 constexpr optional_ref(
std::nullopt_t) noexcept
86 requires (!std::is_same_v<std::remove_cvref_t<U>, optional_ref> && binds_without_temporary<T, U>)
87 constexpr explicit(!std::is_convertible_v<U, T&>) optional_ref(U&& ref)
88 noexcept(std::is_nothrow_constructible_v<T&, U>) :
89 _ptr(
std::addressof(static_cast<T&>(
std::forward<U>(ref))))
94 requires ( !std::is_same_v<std::remove_cvref_t<U>, optional_ref>
95 && !binds_without_temporary<T, U>
96 && std::is_constructible_v<T&, U>
98 optional_ref(U&&) =
delete;
102 template <
typename U>
103 requires (!std::is_same_v<U, T> && std::is_convertible_v<U*, T*>)
104 constexpr optional_ref(
const optional_ref<U>& other) noexcept :
109 constexpr optional_ref& operator=(std::nullopt_t)
noexcept
117 constexpr bool has_value() const noexcept
119 return _ptr !=
nullptr;
124 constexpr explicit operator bool() const noexcept
134 constexpr T& operator*() const noexcept
140 constexpr T* operator->() const noexcept
150 constexpr T& value()
const
153 throw std::bad_optional_access();
160 template <
typename U = std::remove_cv_t<T>>
162 constexpr std::remove_cv_t<T> value_or(U&& fallback)
const
167 return static_cast<std::remove_cv_t<T>
>(std::forward<U>(fallback));
171 template <
typename U>
172 requires binds_without_temporary<T, U>
173 constexpr T& emplace(U&& ref)
noexcept(std::is_nothrow_constructible_v<T&, U>)
175 T& target =
static_cast<T&
>(std::forward<U>(ref));
176 _ptr = std::addressof(target);
181 template <
typename U>
182 requires (!binds_without_temporary<T, U> && std::is_constructible_v<T&, U>)
183 T& emplace(U&&) =
delete;
186 constexpr void reset() noexcept
191 constexpr void swap(optional_ref& other)
noexcept
193 std::swap(_ptr, other._ptr);
196 friend constexpr void swap(optional_ref& a, optional_ref& b)
noexcept
202 friend constexpr bool operator==(
const optional_ref& self, std::nullopt_t)
noexcept
208 template <
typename U>
209 friend class optional_ref;
218 using type = std::optional<T>;
222struct optional_type<T&>
224 using type = optional_ref<T>;
239using optional =
typename detail::optional_type<T>::type;
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
JSONV_PUBLIC void swap(value &a, value &b) noexcept
Swap the values a and b.
typename detail::optional_type< T >::type optional
Represents a value that may or may not be present.