我最近得知我不能:
但我最近也了解到我可以 call decltype
to get the return type of said function
所以一个未定义的函数:
int foo(char, short);
我想知道我是否可以将参数类型与tuple
中的类型相匹配。这显然是一个元编程问题。在这个例子中,我真正拍摄的是decltypeargs
:
enable_if_t<is_same_v<tuple<char, short>, decltypeargs<foo>>, int> bar;
任何人都可以帮助我了解如何制作decltypeargs
吗?
答案 0 :(得分:3)
对于非重载函数,指向函数的指针和指向成员函数的指针,只需执行decltype(function)
即可获得未评估上下文中函数的类型,该类型包含所有参数。
因此,要将参数类型作为元组,您需要的只是很多专业化:
// 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>
,因此上面的内容可以是:
template <auto F>
using decltypeargs = typename function_args<decltype(F)>::type;
您将获得decltypeargs<foo>
语法。