确定未定义函数的参数类型 [英] Determining the Parameter Types of an Undefined Function

查看:340
本文介绍了确定未定义函数的参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近了解到我不能:


  1. 获取未定义函数的地址

  2. 输入地址

但我最近也了解到,我 >可以 调用 decltype 获取所述函数的返回类型

But I've also recently learned that I can call decltype to get the return type of said function

因此一个未定义的函数:

So an undefined function:

int foo(char, short);

我想知道是否有一种方法可以将参数类型与类型匹配a tuple 。这显然是一个元程序设计问题。在这个例子中,我真正拍摄的是 decltypeargs

I'd like to know if there's a way that I can match the parameter types to the types in a tuple. This is obviously a meta programming question. What I'm really shooting for is something like decltypeargs in this example:

enable_if_t<is_same_v<tuple<char, short>, decltypeargs<foo>>, int> bar;

任何人都可以帮助我了解 decltypeargs

Can anyone help me understand how decltypeargs could be crafted?

推荐答案

对于非重载函数,函数指针和指向成员函数的指针,只需要做 decltype(function)给出了在未评估的上下文中函数的类型,该类型包含所有参数。

For non-overloaded functions, pointers to functions, and pointers to member functions, simply doing decltype(function) gives you the type of the function in an unevaluated context, and that type contains all the arguments.

因此,为了将参数类型作为元组,您需要的是很多专业:

So to get the the argument types as a tuple, all you need are a lot of specializations:

// primary for function objects
template <class T>
struct function_args
: function_args<decltype(&T::operator()>
{ };

// normal function
template <class R, class... Args>
struct function_args<R(Args...)> {
    using type = std::tuple<Args...>;
};

// pointer to non-cv-qualified, non-ref-qualified, non-variadic member function
template <class R, class C, class... Args>
struct function_args<R (C::*)(Args...)>
: function_args<R(Args...)>
{ };

// + a few dozen more in C++14
// + a few dozen more on top of that with noexcept being part of the type system in C++17

有了:

template <class T>
using decltypeargs = typename function_args<T>::type;

这需要你写 decltypeargs< decltype(foo)>

使用C ++ 17,我们将有 template< auto> ,因此上面的代码可以是:

With C++17, we will have template <auto>, so the above can be:

template <auto F>
using decltypeargs = typename function_args<decltype(F)>::type;

,你会得到 decltypeargs< foo> 语法。

这篇关于确定未定义函数的参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆