#ifndef PYTHON_PYBIND11_UTILS_SMART_VARIANT_H #define PYTHON_PYBIND11_UTILS_SMART_VARIANT_H #include namespace mo2::python { namespace detail { // simple template class that should be specialized to expose proper fromXXX // methods // template struct smart_variant_converter { template static T from(U&& u) { return T{std::forward(u)}; } }; } // namespace detail // a smart_variant is a std::variant that can be automatically converted to any of // its type via custom operator T() // // user should specialize detail::smart_variant_converter to provide proper // conversions // template struct smart_variant : std::variant { using std::variant::variant; template ...>, int> = 0> operator T() const { return std::visit( [](auto const& t) -> T { if constexpr (std::is_same_v, T>) { return t; } else { return detail::smart_variant_converter::from(t); } }, *this); } }; } // namespace mo2::python #endif