具有专用功能的分体功能类型 [英] 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;
当我研究这里所做的事情时,我试图重写function_args
.我尝试使用一个函数来执行此操作,以消除对decltypeargs
模板的需要.但是发现自己陷入了语法错误:
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...)>();
我的希望是调用decltype(myTry<decltype(foo)>())
来获取tuple
类型,而不必调用decltypeargs<decltype(foo)>
.有没有办法通过函数声明来做到这一点?
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?
推荐答案
//------------------------ 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;
}
这篇关于具有专用功能的分体功能类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文