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

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

Open
jaskij opened this issue Jan 23, 2024 · 2 comments
Open

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

jaskij opened this issue Jan 23, 2024 · 2 comments

Comments

@jaskij
Copy link

jaskij commented Jan 23, 2024

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:

  • in C++17 and newer projects, replace better_enum::optional with std::optional
  • provide a conversion function to std::optional

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
Copy link
Owner

aantron commented Feb 2, 2024

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

@jaskij
Copy link
Author

jaskij commented Feb 2, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants