Extract just the argument type list from decltype(someFunction)

拈花ヽ惹草 提交于 2019-12-05 04:58:32

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;
Cassio Neri

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!