From 55e4aaceb07e72f1b907bc5281a8d185e9114c91 Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Mon, 11 Sep 2023 10:31:41 -0400 Subject: [PATCH] Update optional implementation --- src/platform/common.hpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/platform/common.hpp b/src/platform/common.hpp index d5739bc..0c6e2e9 100644 --- a/src/platform/common.hpp +++ b/src/platform/common.hpp @@ -285,9 +285,9 @@ class optional { }; public: - optional() {} + optional() noexcept {} - optional(nullopt_t) {} + optional(nullopt_t) noexcept {} ~optional() { reset(); @@ -299,7 +299,7 @@ public: } } - optional(optional&& other) : holds_value(other.holds_value) { + optional(optional&& other) noexcept(std::is_nothrow_move_constructible::value) : holds_value(other.holds_value) { if(holds_value) { new (static_cast(std::addressof(uvalue))) T(std::move(other.uvalue)); } @@ -311,7 +311,9 @@ public: return *this; } - optional& operator=(optional&& other) { + optional& operator=(optional&& other) noexcept( + std::is_nothrow_move_assignable::value && std::is_nothrow_move_constructible::value + ) { reset(); if(other.holds_value) { new (static_cast(std::addressof(uvalue))) T(std::move(other.uvalue)); @@ -320,12 +322,18 @@ public: return *this; } - template::type, optional>::value, int>::type = 0> + template< + typename U = T, + typename std::enable_if::type, optional>::value, int>::type = 0 + > optional(U&& value) : holds_value(true) { new (static_cast(std::addressof(uvalue))) T(std::forward(value)); } - template::type, optional>::value, int>::type = 0> + template< + typename U = T, + typename std::enable_if::type, optional>::value, int>::type = 0 + > optional& operator=(U&& value) { if(holds_value) { uvalue = std::forward(value); @@ -336,6 +344,11 @@ public: return *this; } + optional& operator=(nullopt_t) noexcept { + reset(); + return *this; + } + void swap(optional& other) { if(holds_value && other.holds_value) { std::swap(uvalue, other.uvalue);