Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better_enum::optional: add conversion operator to std::optional #122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <iosfwd>
#include <stdexcept>

#if __cplusplus >= 201703L
#include <optional>
#endif


// in-line, non-#pragma warning handling
// not supported in very old compilers (namely gcc 4.4 or less)
Expand Down Expand Up @@ -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<T>() const {
if(has_value()) {return {_value};}
return std::nullopt;
}
#endif

private:
bool _valid;
T _value;
Expand Down