简体   繁体   English

确定作为模板化参数给出的函数的返回类型

[英]Determine the return-type of a function which is given as a templated-parameter

I have a function with a templated parameter which accepts another function. 我有一个带有模板化参数的函数,它接受另一个函数。 Within that function, I'd like to call a different template function which needs to be instantiated with the return-type of function argument. 在该函数中,我想调用一个不同的模板函数,该函数需要使用函数参数的return类型进行实例化。

Since I probably screwed that last paragraph up, let me attempt to clarify with an example: 由于我可能把最后一段搞砸了,让我试着用一个例子来澄清:

template <typename funT>
void foo(funT function_to_call)
{
    auto data = bar<funT::return_value>();
    /// do some stuff with data.
    /// call function_to_call, but bar needed to be called first.
}

How do get funT::return_value ? 怎么得到funT :: return_value?

Many thanks, 非常感谢,

You could use type traits in particular std::result_of in the following manner: 您可以按以下方式使用特定于std::result_of类型特征:

template <typename funT>
void foo(funT function_to_call) {
  auto data = bar<typename std::result_of<decltype(function_to_call)&()>::type>();
  //...
}

LIVE DEMO 现场演示

You could also further generalize to accept any kind of function along with its input arguments by using variadic templates in the following manner: 您还可以通过以下方式使用可变参数模板进一步概括接受任何类型的函数及其输入参数:

template <typename funT, typename ...Args>
void foo(funT function_to_call, Args... args) {
  auto data = bar<typename std::result_of<funT(Args...)>::type>();
  ...
}

LIVE DEMO 现场演示

Apart from using result_of as others have suggested, you can also use decltype . 除了使用其他人建议的result_of之外,您还可以使用decltype

For the case where function_to_call accepts no parameters, you can do the following: 对于function_to_call接受任何参数的情况,您可以执行以下操作:

auto data = bar<decltype(function_to_call())>();

However, for a more generic case, as @101010 has pointed out, you can have your function accept any number of arguments. 但是,对于更通用的情况,正如@ 101010指出的那样,您可以让您的函数接受任意数量的参数。 The resulting code would look like this: 生成的代码如下所示:

template <typename funT, typename ...Args>
void foo(funT function_to_call, Args&&... args) 
{
   auto data = bar<decltype(function_to_call(std::forward<Args>(args)...))>();
}

For the cases that I've tried, decltype and std::result_of have the same functionality with regards to returning the correct type if the function type being passed in isn't a pointer-to-member, as @hvd pointed out. 对于我尝试过的情况,如果传入的函数类型不是指向成员的指针,则decltypestd::result_of在返回正确的类型方面具有相同的功能,如@hvd指出的那样。 Looking looking through the g++ source, std::result_of is often implemented in terms of decltype for the case described above. 通过查看g ++源代码, std::result_of通常根据上述情况的decltype实现。

Using this seems much cleaner and more readable to me than the typename std::result_of<...>::type alternative, although the C++14 std::result_of_t option is quite attractive as well. 使用它看起来比typename std::result_of<...>::type替代方案更清晰,更易读,尽管C ++ 14 std::result_of_t选项也非常有吸引力。

您可以使用typename std::result_of<funT()>::type ,以满足您的需求,或者std::result_of_t<funT()>如果你有机会到C ++ 14。

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

相关问题 有没有办法根据函数参数值动态更改 C++ 中函数的返回类型? - Is there a way to dynamically change the return-type of a function in C++ based on function parameter values? 具有void返回类型和模板化参数的std :: function - std::function with void return type and templated parameter 为什么解析了模板化函数参数而不解析了模板化返回类型? - Why is a templated function parameter resolved but not a templated return type? 声明一个向量返回类型函数并返回向量 - declare a vector return-type function and return the vector 传递具有模板化返回类型的std :: function类型 - Passing std::function type which has a templated return type C ++:在头文件中声明struct return-type函数 - C++ : Declaring struct return-type function in header file 给定传递给它的参数类型,如何确定函数参数的类型? - How to determine the type of a function parameter given the type of argument passed to it? 如何确定模板化函数中接收的类型是列表? - How to determine that a type received in the templated function is a list? 确保模板化通用参数的返回类型 - Ensure return type of templated generic parameter 如何在返回类型功能模板的专业化中使用派生类型? (“无法推断模板参数”) - How to use derived type in specialisation of return-type function template? (“couldn't infer template argument”)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM