32 static_assert(!std::is_reference<T>::value,
"Do not use a reference for T");
34 constexpr OptionalRef()
noexcept =
default;
35 constexpr OptionalRef(std::nullopt_t)
noexcept {}
36 constexpr OptionalRef(
const OptionalRef&)
noexcept =
default;
39 constexpr OptionalRef(T& other)
noexcept : data_(std::addressof(other)) {}
42 explicit constexpr OptionalRef(
const T&&) =
delete;
45 explicit constexpr OptionalRef(
const std::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
48 explicit constexpr OptionalRef(std::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
51 explicit constexpr OptionalRef(
const std::optional<U>&&)
noexcept {
52 static_assert(!
sizeof(U),
"Forming a reference to a temporary");
56 explicit constexpr OptionalRef(
const boost::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
59 explicit constexpr OptionalRef(boost::optional<U>& other)
noexcept : data_(GetPointer(other)) {}
62 explicit constexpr OptionalRef(
const boost::optional<U>&&)
noexcept {
63 static_assert(!
sizeof(U),
"Forming a reference to a temporary");
66 constexpr bool has_value()
const noexcept {
return !!data_; }
67 constexpr explicit operator
bool()
const noexcept {
return has_value(); }
69 constexpr T* operator->()
const {
74 constexpr T& operator*()
const {
79 constexpr T& value()
const {
81 throw std::bad_optional_access();
88 constexpr T value_or(U&& default_value)
const {
90 return std::forward<U>(default_value);
97 template <
class Optional>
98 static T* GetPointer(Optional& other)
noexcept {
99 using ValueType =
decltype(*other);
101 std::is_const<T>::value || !std::is_const<ValueType>::value,
102 "Attempt to initialize non-const T from a const optional value"
105 return other.has_value() ? std::addressof(*other) :
nullptr;
108 T*
const data_ =
nullptr;