Skip to content

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

@jaskij

Description

@jaskij

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions