Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 951
gimmeamilk Avatar asked Sep 18 '13 11:09

gimmeamilk


2 Answers

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;
like image 165
Daniel Frey Avatar answered Oct 03 '22 04:10

Daniel Frey


Here's an alternative (borrowing ideas from Daniel Frey's solution) using a function template rather than a class template:

template <template<typename...> class C, typename R, typename... Args>
C<Args...> apply_args(R(Args...));

void f(int, bool);

using MyPair = decltype(apply_args<std::pair>(f)); // = std::pair<int, bool>
MyPair myPair{42, false};

Edit: Comments on my solution x Daniel Frey's:

Mine saves typing. His is more idiomatic. Indeed, in C++ metaprogramming a "function" (or a meta-function) that takes types and return a type is (generally) implemented as a template classe whose member type gives the return. For this reason, I prefer his solution.

like image 28
Cassio Neri Avatar answered Oct 03 '22 05:10

Cassio Neri