diff --git a/Siv3D/include/Siv3D/Optional.hpp b/Siv3D/include/Siv3D/Optional.hpp index 5fddeeee9..fc6c0217b 100644 --- a/Siv3D/include/Siv3D/Optional.hpp +++ b/Siv3D/include/Siv3D/Optional.hpp @@ -95,16 +95,22 @@ namespace s3d constexpr value_type value_or_eval(Fty&& f)&&; template >* = nullptr> - constexpr void then(Fty&& f); + constexpr void then(Fty&& f)&; template >* = nullptr> - constexpr void then(Fty&& f) const; + constexpr void then(Fty&& f) const&; + + template >* = nullptr> + constexpr void then(Fty&& f)&&; template >> - Optional map(Fty f); + Optional map(Fty f)&; + + template >> + Optional map(Fty f) const&; template >> - Optional map(Fty f) const; + Optional map(Fty f)&&; template friend std::basic_ostream& operator <<(std::basic_ostream& output, const Optional& value) diff --git a/Siv3D/include/Siv3D/detail/Optional.ipp b/Siv3D/include/Siv3D/detail/Optional.ipp index 673e85296..98452f8cb 100644 --- a/Siv3D/include/Siv3D/detail/Optional.ipp +++ b/Siv3D/include/Siv3D/detail/Optional.ipp @@ -122,7 +122,14 @@ namespace s3d [[nodiscard]] inline constexpr typename Optional::value_type Optional::value_or_construct(Args&&... args) const& { - return static_cast(*this) ? **this : Type(std::forward(args)...); + if (*this) + { + return **this; + } + else + { + return Type(std::forward(args)...); + } } template @@ -130,26 +137,47 @@ namespace s3d [[nodiscard]] inline constexpr typename Optional::value_type Optional::value_or_construct(Args&&... args)&& { - return static_cast(*this) ? std::move(**this) : Type(std::forward(args)...); + if (*this) + { + return std::move(**this); + } + else + { + return Type(std::forward(args)...); + } } template template , std::is_convertible, Type>>>*> inline constexpr typename Optional::value_type Optional::value_or_eval(Fty&& f) const& { - return static_cast(*this) ? **this : std::forward(f)(); + if (*this) + { + return **this; + } + else + { + return std::forward(f)(); + } } template template , std::is_convertible, Type>>>*> inline constexpr typename Optional::value_type Optional::value_or_eval(Fty&& f)&& { - return static_cast(*this) ? std::move(**this) : std::forward(f)(); + if (*this) + { + return std::move(**this); + } + else + { + return std::forward(f)(); + } } template template >*> - inline constexpr void Optional::then(Fty&& f) + inline constexpr void Optional::then(Fty&& f)& { if (has_value()) { @@ -159,7 +187,7 @@ namespace s3d template template >*> - inline constexpr void Optional::then(Fty&& f) const + inline constexpr void Optional::then(Fty&& f) const& { if (has_value()) { @@ -167,9 +195,19 @@ namespace s3d } } + template + template >*> + inline constexpr void Optional::then(Fty&& f)&& + { + if (has_value()) + { + std::forward(f)(std::move(*this).value()); + } + } + template template - inline Optional Optional::map(Fty f) + inline Optional Optional::map(Fty f)& { if (has_value()) { @@ -183,7 +221,7 @@ namespace s3d template template - inline Optional Optional::map(Fty f) const + inline Optional Optional::map(Fty f) const& { if (has_value()) { @@ -195,6 +233,20 @@ namespace s3d } } + template + template + inline Optional Optional::map(Fty f)&& + { + if (has_value()) + { + return f(std::move(*this).value()); + } + else + { + return none; + } + } + template inline constexpr bool operator ==(const Optional& lhs, const Optional& rhs) {