From eb701714334ce3ced479204e93a3ed4894a4af50 Mon Sep 17 00:00:00 2001 From: Chris Robinson Date: Mon, 1 Jul 2019 16:27:49 -0700 Subject: Add a few more constructor and assignment operators for optional --- common/aloptional.h | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/aloptional.h b/common/aloptional.h index 443da0b4..a4b37212 100644 --- a/common/aloptional.h +++ b/common/aloptional.h @@ -34,9 +34,25 @@ public: if(mHasValue) al::uninitialized_move_n(std::addressof(*rhs), 1, std::addressof(mValue)); } - template - explicit optional(in_place_t, Args&& ...args) - : mHasValue{true}, mValue{std::forward(args)...} + template::value)> + explicit optional(in_place_t, Args&& ...args) : mHasValue{true} + , mValue{std::forward(args)...} + { } + template&, Args...>::value)> + explicit optional(in_place_t, std::initializer_list il, Args&& ...args) + : mHasValue{true}, mValue{il, std::forward(args)...} + { } + template::value && + !std::is_same::type, in_place_t>::value && + !std::is_same::type, optional>::value && + std::is_constructible::value)> + constexpr explicit optional(U&& value) : mHasValue{true}, mValue{std::forward(value)} + { } + template::value && + !std::is_same::type, in_place_t>::value && + !std::is_same::type, optional>::value && + !std::is_constructible::value)> + constexpr optional(U&& value) : mHasValue{true}, mValue{std::forward(value)} { } ~optional() { reset(); } @@ -69,6 +85,22 @@ public: } return *this; } + template::value && + std::is_assignable::value && + !std::is_same::type, optional>::value && + (!std::is_same::type, T>::value || + !std::is_scalar::value))> + optional& operator=(U&& rhs) + { + if(*this) + mValue = std::forward(rhs); + else + { + ::new (std::addressof(mValue)) T{std::forward(rhs)}; + mHasValue = true; + } + return *this; + } const T* operator->() const { return std::addressof(mValue); } T* operator->() { return std::addressof(mValue); } -- cgit v1.2.3