From 330935c1d679d45060df6e71d557561f18260cc6 Mon Sep 17 00:00:00 2001 From: jaskij Date: Sat, 10 Feb 2024 17:49:05 +0100 Subject: [PATCH] better_enum::optional: add conversion operator to std::optional --- enum.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/enum.h b/enum.h index c89804b..b533744 100644 --- a/enum.h +++ b/enum.h @@ -14,6 +14,10 @@ #include #include +#if __cplusplus >= 201703L +#include +#endif + // in-line, non-#pragma warning handling // not supported in very old compilers (namely gcc 4.4 or less) @@ -344,6 +348,13 @@ struct optional { BETTER_ENUMS_CONSTEXPR_ const T& value() const { return _value; } + #if defined(__cpp_lib_optional) && (__cpp_lib_optional >= 201606L) + BETTER_ENUMS_CONSTEXPR_ operator std::optional() const { + if(has_value()) {return {_value};} + return std::nullopt; + } + #endif + private: bool _valid; T _value;