我可以从签名中获得函数的返回类型吗? [英] 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);
如您所见,所有这些decltype(declval<T>().x)
都很难阅读.我可以模板化using
还是有一些标准函数可以让我从函数指针中提取返回类型,而无需将参数类型传递给decltype
或result_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);
推荐答案
在 c ++ 17 function
对象已获得推断指南,它可以根据传递给构造函数的参数来确定其类型.因此,例如,给定
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 只要我们简单地得出bar
的function<int()>
类型:
In c++17 bar
's function<int()>
type will be derived if we simply:
function bar(foo);
因此,我们可以使用推导指南使用仅 签名来填充临时function
;从而使用function
的result_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屋!