Search code examples
c++templatesc++11decltypevariadic

Extract just the argument type list from decltype(someFunction)


I have a variadic template that represents a list of parameters for a function, eg:

void myFunc (int,int,std::string) { }
template<typename... Args> class MyTemplateClass { };
...
MyTemplateClass<int,int,std::string> myConcrete; // for use with myFunc later

Is there any way I can extract just the argument types from decltype(func) to save having to write them manually, eg:

MyTemplateClass<something_like_decltype(myFunc)> myConcrete;

ie decltype in this case would give me "void(int,int,string)" but is there a way of extracting just the "int,int,string" part for use in the variadic template?

Note: I must use the variadic template method because within the template it performs processing on each argument type in turn.


Solution

  • The following should work:

    template<template<typename...> class C,typename T>
    struct apply_args;
    
    template<template<typename...> class C,typename R,typename... Args>
    struct apply_args<C, R(Args...) >
    {
        typedef C<Args...> type;
    };
    
    typedef apply_args<MyTemplateClass,decltype(myFunc)>::type MyConcrete;
    MyConcrete myConcrete;