Search code examples
c++boost

Convert boost::parameter_types to std::tuple


I believe there are some ways to do this with MPL and fusion, but since I'm new to boost, it's kinda hard to figure out.

It doesn't matter whether the tuple is of std's or boost's, what I'm trying to ultimately achieve is:

template<T>
void some_func()
{
    // declare a tuple contains types of which in boost::function_types::parameter_types<T>
    // insert values to the tuple
    // call std::apply()
}

I've tried using fold, but failed to figure out what should be in the third template argument.


Solution

  • Haven't tested it, but the following should work:

    #include <type_traits>
    #include <tuple>
    #include <boost/mpl/at.hpp> // For at
    
    namespace detail {
      template<class ParamSequence, std::size_t ...Indices>
      auto unpack_params(ParamSequence, std::index_sequence<Indices...>) -> std::tuple<boost::mpl::at<ParamSequence, Indices>...>;
    }
    
    template<class Function>
    using param_tuple_t = decltype(detail::unpack_params(boost::function_types::parameter_types<Function>(), std::make_index_sequence<boost::function_types::function_arity<Function>::value>()>()));