20 #ifndef _OPTIONAL_HPP_
21 #define _OPTIONAL_HPP_
25 #if (__cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L))
30 #include <type_traits>
38 optional() noexcept: initialized_(false) {}
40 optional(
const T& value) noexcept(std::is_nothrow_copy_constructible<T>::value) {
41 new (&value_) T(value);
45 optional(T&& value) noexcept(std::is_nothrow_move_constructible<T>::value) {
46 new (&value_) T(std::move(value));
52 optional(
const optional<TT>& other) noexcept(std::is_nothrow_constructible<T, TT>::value): initialized_(false) {
53 if (other.initialized_) {
54 new (&value_) T(other.value_);
59 optional(
const optional& other) noexcept(std::is_nothrow_copy_constructible<T>::value): initialized_(false) {
60 if (other.initialized_) {
61 new (&value_) T(other.value_);
66 optional(optional&& other) noexcept(std::is_nothrow_move_constructible<T>::value): initialized_(false) {
67 if (other.initialized_) {
68 new (&value_) T(std::move(other.value_));
73 ~optional() noexcept(std::is_nothrow_destructible<T>::value) {
74 if (initialized_) value_.~T();
77 explicit operator bool() const noexcept {
81 optional& operator=(
const optional& other)
82 noexcept(std::is_nothrow_copy_constructible<T>::value && std::is_nothrow_copy_assignable<T>::value) {
84 if (other.initialized_) {
85 value_ = other.value_;
90 if (other.initialized_) {
91 new (&value_) T(other.value_);
98 optional& operator=(optional&& other)
99 noexcept(std::is_nothrow_move_constructible<T>::value && std::is_nothrow_move_assignable<T>::value) {
101 if (other.initialized_) {
102 value_ = std::move(other.value_);
107 if (other.initialized_) {
108 new (&value_) T(std::move(other.value_));
115 template<
typename... Args>
116 void emplace(Args&&... args) noexcept(std::is_nothrow_constructible<T, Args...>::value) {
117 new (&value_) T(args...);
121 T& operator*() & noexcept {
return value_; }
122 const T& operator*() const & noexcept {
return value_; }
123 T&& operator*() && noexcept {
return std::move(value_); }
124 const T&& operator*() const && noexcept {
return std::move(value_); }
126 T* operator->() noexcept {
return &value_; }
127 const T* operator->() const noexcept {
return &value_; }
129 void reset() noexcept(std::is_nothrow_destructible<T>::value) {
130 if (initialized_) value_.~T();
131 initialized_ =
false;
141 template<
typename TT>
friend class optional;
DataSketches namespace.
Definition: binomial_bounds.hpp:38