[英]Can I get the Return Type of a Function From a Signature?
So I have a ton of functions similar to these: 所以我有很多类似于以下的功能:
template <typename T>
bool Zero(const T, const T, const T);
template <typename T>
T One(const T, const T, const T, bool);
template <typename T>
T Three(const T, const T, const T, const T, const T, const T);
For each of these functions I have a wrapper which uses the return type of these functions so it looks something like this: 对于这些函数中的每一个,我都有一个包装器,该包装器使用这些函数的返回类型,因此看起来像这样:
template <typename T>
decltype(Zero<decltype(declval<T>().x)>(decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()))) ZeroWrapper(const T);
template <typename T>
decltype(One<decltype(declval<T>().x)>(decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()), bool())) OneWrapper(const T);
template <typename T>
decltype(Three<decltype(declval<T>().x)>(decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()), decltype(declval<decltype(declval<T>().x)>()))) ThreeWrapper(const T);
As you can see all those decltype(declval<T>().x)
's get disgustingly hard to read. 如您所见,所有这些
decltype(declval<T>().x)
很难阅读。 Can I template a using
or is there some standard function which will allow me to extract the return type from a function pointer without passing the argument types to decltype
or result_of
? 我可以模板化
using
还是有一些标准函数,这些函数可以让我从函数指针中提取返回类型,而无需将参数类型传递给decltype
或result_of
? So something like this: 所以像这样:
template <typename T>
foo_t<Zero<decltype(declval<T>().x)>> ZeroWrapper(const T);
template <typename T>
foo_t<One<decltype(declval<T>().x)>> OneWrapper(const T);
template <typename T>
foo_t<Three<decltype(declval<T>().x)>> ThreeWrapper(const T);
Can I template a using or is there some standard function which will allow me to extract the return type from a function pointer without passing the argument types to
decltype
orresult_of
?我可以模板化using还是有一些标准函数,这些函数可以让我从函数指针中提取返回类型,而无需将参数类型传递给
decltype
或result_of
?
Yes! 是!
#include <tuple>
#include <functional>
template<class T>
struct callable_trait
{};
template<class R, class... Args>
struct callable_trait<std::function<R(Args...)>>
{
using return_type = R;
using argument_types = std::tuple<Args...>;
};
template<auto callable>
using return_type = typename callable_trait<decltype(std::function{callable})>::return_type;
return_type<some_callable>
is the type returned by some_callable
when called with appropriate arguments. return_type<some_callable>
是使用适当参数调用时some_callable
返回的类型。 This uses a std::function
in order to provide a specialization for each possible kind of callable (free function, function pointer, member function, functor object). 这使用
std::function
来为每种可能的可调用类型(自由函数,函数指针,成员函数,函子对象)提供专门化。 This is explained in this StackOverflow answer . 这在此StackOverflow答案中进行了解释 。
In your case, you can use it like so: 在您的情况下,可以这样使用它:
template <typename T>
bool Zero(const T, const T, const T);
template <typename T>
T One(const T, const T, const T, bool);
template <typename T>
T Three(const T, const T, const T, const T, const T, const T);
template <typename T>
return_type<Zero<T>> ZeroWrapper(const T);
template <typename T>
return_type<One<T>> OneWrapper(const T);
template <typename T>
return_type<Three<T>> ThreeWrapper(const T);
In c++17 the function
object has been endowed with a Deduction Guide which allows it to determine it's type from the argument passed to the constructor. 在c ++ 17中,该
function
对象具有推导指南 ,该指南允许其根据传递给构造函数的参数确定其类型。 So for example, given the function int foo()
in c++11 we had to do: 因此,例如,给定c ++ 11中的
int foo()
函数,我们必须这样做:
function<int()> bar(foo);
In c++17 bar
's function<int()>
type will be derived if we simply: 在c ++ 17
bar
如果我们简单地得出以下类型,就会派生出function<int()>
类型:
function bar(foo);
Thus we can use the Deduction Guide to populate a temporary function
with only the signature; 因此,我们可以使用推导指南仅用签名来填充临时
function
; thereby using function
's result_type
to find the result of your helper funcitons: 从而使用
function
的result_type
来查找辅助function
的结果:
template <typename T>
typename decltype(function(Zero<decltype(declval<T>().x)>))::return_type ZeroWrapper(const T);
template <typename T>
typename decltype(function(One<decltype(declval<T>().x)>))::return_type OneWrapper(const T);
template <typename T>
typename decltype(function(Three<decltype(declval<T>().x)>))::return_type ThreeWrapper(const T);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.