mirror of
https://github.com/aantron/better-enums.git
synced 2025-12-06 16:56:42 +08:00
better_enum::optional: add conversion operator to std::optional
This commit is contained in:
parent
520d8ee390
commit
330935c1d6
11
enum.h
11
enum.h
@ -14,6 +14,10 @@
|
|||||||
#include <iosfwd>
|
#include <iosfwd>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
|
#if __cplusplus >= 201703L
|
||||||
|
#include <optional>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// in-line, non-#pragma warning handling
|
// in-line, non-#pragma warning handling
|
||||||
// not supported in very old compilers (namely gcc 4.4 or less)
|
// 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; }
|
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:
|
private:
|
||||||
bool _valid;
|
bool _valid;
|
||||||
T _value;
|
T _value;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user