问题
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.
回答1:
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;
回答2:
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.
来源:https://stackoverflow.com/questions/18870918/extract-just-the-argument-type-list-from-decltypesomefunction