From 5c82ab1e0dbc4dca189ed53cf1759c16feade102 Mon Sep 17 00:00:00 2001 From: _Redstone_c_ Date: Wed, 13 Apr 2022 22:35:30 +0800 Subject: [PATCH] refactor(templates): refining TOptional's constraints --- .../Source/Public/Templates/Optional.h | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/Redcraft.Utility/Source/Public/Templates/Optional.h b/Redcraft.Utility/Source/Public/Templates/Optional.h index 4349a20..1b65418 100644 --- a/Redcraft.Utility/Source/Public/Templates/Optional.h +++ b/Redcraft.Utility/Source/Public/Templates/Optional.h @@ -14,6 +14,20 @@ NAMESPACE_MODULE_BEGIN(Utility) template requires TIsObject::Value && (!TIsArray::Value) && TIsDestructible::Value struct TOptional { +private: + + template + struct TAllowUnwrapping : TBoolConstant& >::Value + || TIsConstructible& >::Value + || TIsConstructible&&>::Value + || TIsConstructible&&>::Value + || TIsConvertible< TOptional&, OptionalType>::Value + || TIsConvertible&, OptionalType>::Value + || TIsConvertible< TOptional&&, OptionalType>::Value + || TIsConvertible&&, OptionalType>::Value + )> { }; + public: using ValueType = OptionalType; @@ -47,14 +61,14 @@ public: if (InValue.IsValid()) new(&Value) OptionalType(MoveTemp(InValue.GetValue())); } - template requires TIsConstructible::Value + template requires TIsConstructible::Value && TAllowUnwrapping::Value constexpr explicit (!TIsConvertible::Value) TOptional(const TOptional& InValue) : bIsValid(InValue.IsValid()) { if (InValue.IsValid()) new(&Value) OptionalType(InValue.GetValue()); } - template requires TIsConstructible::Value + template requires TIsConstructible::Value && TAllowUnwrapping::Value constexpr explicit (!TIsConvertible::Value) TOptional(TOptional&& InValue) : bIsValid(InValue.IsValid()) { @@ -106,7 +120,7 @@ public: return *this; } - template requires TIsConstructible::Value && TIsAssignable::Value + template requires TIsConstructible::Value && TIsAssignable::Value && TAllowUnwrapping::Value constexpr TOptional& operator=(const TOptional& InValue) { if (&InValue == this) return *this; @@ -127,7 +141,7 @@ public: return *this; } - template requires TIsConstructible::Value && TIsAssignable::Value + template requires TIsConstructible::Value && TIsAssignable::Value && TAllowUnwrapping::Value constexpr TOptional& operator=(TOptional&& InValue) { if (&InValue == this) return *this; @@ -219,7 +233,7 @@ private: }; template -TOptional(T) ->TOptional; +TOptional(T) -> TOptional; template requires CWeaklyEqualityComparableWith constexpr bool operator==(const TOptional& LHS, const TOptional& RHS)