我可以从签名中获得函数的返回类型吗? [英] Can I get the Return Type of a Function From a Signature?

查看:130
本文介绍了我可以从签名中获得函数的返回类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有很多类似的功能:

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);

如您所见,所有这些decltype(declval<T>().x)都很难阅读.我可以模板化using还是有一些标准函数可以让我从函数指针中提取返回类型,而无需将参数类型传递给decltyperesult_of?像这样:

As you can see all those decltype(declval<T>().x)'s get disgustingly hard to read. 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? 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);

推荐答案

对象已获得推断指南,它可以根据传递给构造函数的参数来确定其类型.因此,例如,给定int foo()"class =" post-tag"title ="显示标记为'c ++ 11'的问题rel =" tag> c ++ 11 我们必须要做的:

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. So for example, given the function int foo() in c++11 we had to do:

function<int()> bar(foo);

c ++ 17 只要我们简单地得出barfunction<int()>类型:

In c++17 bar's function<int()> type will be derived if we simply:

function bar(foo);

因此,我们可以使用推导指南使用仅 签名来填充临时function;从而使用functionresult_type查找辅助函数的结果:

Thus we can use the Deduction Guide to populate a temporary function with only the signature; thereby using function's result_type to find the result of your helper funcitons:

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);

实时示例

这篇关于我可以从签名中获得函数的返回类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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