繁体   English   中英

我可以从签名中获得函数的返回类型吗?

[英]Can I get the Return Type of a Function From a Signature?

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

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>
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 所以像这样:

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

我可以模板化using还是有一些标准函数,这些函数可以让我从函数指针中提取返回类型,而无需将参数类型传递给decltyperesult_of

是!

#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>是使用适当参数调用时some_callable返回的类型。 这使用std::function来为每种可能的可调用类型(自由函数,函数指针,成员函数,函子对象)提供专门化。 在此StackOverflow答案中进行了解释


在您的情况下,可以这样使用它:

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

完整演示

function对象具有推导指南 ,该指南允许其根据传递给构造函数的参数确定其类型。 因此,例如,给定 int foo()函数,我们必须这样做:

function<int()> bar(foo);

bar如果我们简单地得出以下类型,就会派生出function<int()>类型:

function bar(foo);

因此,我们可以使用推导指南仅用签名来填充临时function 从而使用functionresult_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM