c++templatesmetaprogrammingtemplate-functiontemplate-classes

Pull Apart Function Type With Specialized Function


The answer to this question picks apart a function type using a class template:

template <typename T>
struct function_args {};

template <typename R, typename... Args>
struct function_args<R(Args...)> {
    using type = tuple<Args...>;
};

template <typename T>
using decltypeargs = typename function_args<T>::type;

As I studied what was being done here I tried to rewrite function_args. I attempted to do this using a function so as to eliminate the need for the decltypeargs template. But found myself mired in improper syntax:

template <typename T>
tuple<> myTry();

template <typename Ret, typename... Args>
tuple<Args...> myTry<Ret(Args...)>();

My hope had been to call decltype(myTry<decltype(foo)>()) to get the tuple type instead of having to call decltypeargs<decltype(foo)>. Is there a way to do this with a function declaration?


Solution

  • //------------------------ Machinery:
    #include <tuple>
    
    template< class Ret, class... Args >
    std::tuple<Args...> m( Ret(Args...) );
    
    //------------------------- Example:
    #include <iostream>
    #include <typeinfo>
    
    void foo( double );
    
    using namespace std;
    auto main()
        -> int
    {
        using Args_tuple = decltype( m( foo ) );
        cout << typeid( Args_tuple ).name() << endl;
    }