简体   繁体   English

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

[英]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还是有一些标准函数,这些函数可以让我从函数指针中提取返回类型,而无需将参数类型传递给decltyperesult_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 or result_of ? 我可以模板化using还是有一些标准函数,这些函数可以让我从函数指针中提取返回类型,而无需将参数类型传递给decltyperesult_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);

Full demo 完整演示

In 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. function对象具有推导指南 ,该指南允许其根据传递给构造函数的参数确定其类型。 So for example, given the function int foo() in we had to do: 因此,例如,给定 int foo()函数,我们必须这样做:

function<int()> bar(foo);

In bar 's function<int()> type will be derived if we simply: 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: 从而使用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);

Live Example 现场例子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在.cpp 文件中 function 的返回类型(签名)中使用在 header 文件中定义的别名? - How can I use an alias thats defined in a header file, in the return type (signature) of a function in the .cpp file? 我可以让返回类型 auto 与具有相同签名但不同捕获的 lambda 一起使用吗? - Can I get return type auto to work with lambdas of the same signature but different captures? 返回类型是函数签名的一部分吗? - Is the return type part of the function signature? 如何从 function 指针类型获取签名类型? - How to get the the signature type from the function pointer type? 我可以从(旧式)function 签名中获取参数包吗? - Can I get a parameter pack from an (old-style) function signature? 如何从子类中提取嵌套类型并在抽象父类中的虚函数的函数签名中使用它? - How can I extract a nested type from a child class and use it in the function signature of a virtual function in the abstract parent class? 我可以使用 decltype 获取函数的签名吗? - Can I get a function's signature using decltype? 如何在不导致实例化的情况下获取函数模板的签名? - How can I get the signature of a function template without causing instantiation? 我们可以在基础 class 中声明具有相同签名但返回类型不同的 function 吗? - Can we declare a function with the same signature but different return type in the base class? 如何从调用表达式中获取函数的类型? - How can I get the function's type from a call expression?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM