aantron / better-enums

C++ compile-time enum to string, iteration, in a single header file
http://aantron.github.io/better-enums
BSD 2-Clause "Simplified" License
1.67k stars 173 forks source link

C++17 interop: std::optional #120

Open jaskij opened 9 months ago

jaskij commented 9 months ago

In my embedded project, I utilize std::optional heavily. Unfortunately, better_enum::optional doesn't have a built in conversion.

I can see two options:

Here's a member conversion function I threw together quickly:

    #if defined(__cpp_lib_optional) && (__cpp_lib_optional >= 201606L)
    BETTER_ENUMS_CONSTEXPR_ operator std::optional<T>() const {
        if(*this) {return {_value};}
        else {return std::nullopt;}
    }
    #endif
aantron commented 9 months ago

Thank you! Would you be willing to propose this as a PR?

jaskij commented 9 months ago

Sure thing. I wasn't sure if that quickly put together operator was good enough, but if it is, I'll go ahead and submit a PR.