5#define __XTD_STD_INTERNAL__
7#undef __XTD_STD_INTERNAL__
9#if defined(__xtd__cpp_lib_optional)
23 template <
typename value_t>
26 alignas(value_t)
unsigned char data_[
sizeof(value_t)];
32 optional(
const value_t& value) : has_value_(true) {
33 new (data_) value_t(value);
36 optional(value_t&& value) : has_value_(true) {
37 new (data_) value_t(std::move(value));
40 optional(
const optional& other) : has_value_(
other.has_value_) {
41 if (
other.has_value_) {
42 new (data_) value_t(*other);
47 if (
other.has_value_) {
48 new (data_) value_t(std::move(*other));
53 optional& operator=(
const value_t& value) {
55 *
reinterpret_cast<value_t*
>(data_) = value;
57 new (data_) value_t(value);
63 optional& operator=(value_t&& value) {
65 *
reinterpret_cast<value_t*
>(data_) = std::move(value);
67 new (data_) value_t(std::move(value));
73 optional& operator=(
const optional& other) {
78 if (
other.has_value_) {
80 *
reinterpret_cast<value_t*
>(data_) = *other;
82 new (data_) value_t(*other);
91 optional& operator=(optional&& other) {
96 if (
other.has_value_) {
98 *
reinterpret_cast<value_t*
>(data_) = std::move(*other);
100 new (data_) value_t(std::move(*other));
114 bool has_value()
const {
118 const value_t& value()
const {
122 const value_t& value_or(
const value_t& other)
const {
123 return has_value() ? operator*() :
other;
127 if (!has_value())
return;
128 reinterpret_cast<value_t*
>(data_)->~value_t();
132 const value_t* operator->()
const {
134 throw std::runtime_error(
"optional does not contain a value.");
136 return reinterpret_cast<const value_t*
>(data_);
139 value_t* operator->() {
141 throw std::runtime_error(
"optional does not contain a value.");
143 return reinterpret_cast<value_t*
>(data_);
146 const value_t& operator*()
const {
147 return *operator->();
150 value_t& operator*() {
151 return *operator->();
154 explicit operator bool()
const {
180 template<
typename type_t>
Contains __xtd_std_version definitions.
std::optional< type_t > optional
Represents the null_opt alias on std::nullopt_t.
Definition optional.h:181
@ other
The operating system is other.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::optional type.