c++boostboost-hana

boost::hana how to check if a type is std::optional?


I searched the boost::hana::traits I cannot find anything related to checking concept of types. For example is_vector, is_optional. Is there any tools in hana can simplify this? Moreover, I would like to branch the code based on the result thus it needs to be used in hana::eval_if. Can anyone show me some examples?

I would like follow to work:

hana::eval_if(
    is_vector(hana::decltype_(val)),
    [&](auto _) {std::cout << "is vector\n";},
    [&](auto _) {
        hana::eval_if(
                is_optional(hana::decltype_(val)),
                [&](auto _) { std::cout << "is optional\n"; },
                [&](auto _) { std::cout << "neither vector nor optional\n"; }
        );
    }
);

Solution

  • Now with concepts in c++20, the following is possible:

    #include <concepts>
    #include <optional>
    
    template <typename T>
    concept any_optional = std::same_as<T, std::optional<typename T::value_type>>;