简体   繁体   English

如何检索要在模板中使用的函数的返回类型?

[英]How can I retrieve the return type of a function to use in a template?

I have a function somewhere called x that returns a know value and has known parameters: 我在某处有一个函数x ,该函数返回一个已知值并具有已知参数:

int x(int y);

I have somewhere else, I want to create a container to contain n invocations of this function. 我在其他地方,我想创建一个容器来包含此函数的n调用。 Then I want to execute it that many times. 然后,我要执行多次。

The problem is, I don't want to rely on it being an int return type. 问题是,我不想依靠它作为int返回类型。 I need to deduce the return type at compile time. 我需要在编译时推断出返回类型。 Something like: 就像是:

std::vector<result_of<x(int)>::type> results;

But I don't want to have to specify the parameter values because they're static. 但我不想指定参数值,因为它们是静态的。

You can create your own traits, something like: 您可以创建自己的特征,例如:

template <typename F> struct my_result_of;

template <typename F> struct my_result_of<F*> : my_result_of<F> {};

template <typename Ret, typename ... Ts>
struct my_result_of<Ret(Ts...)>
{
    using type = Ret;
};

template <typename F> using my_result_of_t = typename my_result_of<F>::type;

And use it like (assuming no overloads of x ): 并使用它(假设x没有重载):

std::vector<my_result_of_t<decltype(x)>::type> results;

You are close. 你近了 Assuming T is the template argument of the caller function: 假设T是调用方函数的模板参数:

std::vector<decltype(x(std::declval<T>()))> results;

You can abuse std::function::result_type : 您可以滥用std::function::result_type

int x(int y);
static_assert(std::is_same_v<int,std::function<decltype(x)>::result_type>);

Of course this will only work if x is really a function. 当然,只有在x确实是一个函数时,这才起作用。 If x is an arbitrary function object, then its result type may depend on its argument type, in which case you cannot know its result type without specifying the arguments. 如果x是任意函数对象,则其结果类型可能取决于其参数类型,在这种情况下,如果不指定参数就无法知道其结果类型。

I assume you can use up to the most recent standard revision, for you didn't specify it. 我假设您可以使用最新的标准修订版,因为您没有指定它。
Here is a minimal, working example: 这是一个最小的工作示例:

#include<vector>
#include<functional>
#include<utility>

template<std::size_t... I, typename F, typename... A>
auto gen(std::index_sequence<I...>, F &&f, A... args) {
    return std::vector<decltype(std::forward<F>(f)(args...))>{
        (I, std::forward<F>(f)(args...))...
    };
}

template<std::size_t N, typename F, typename... A>
auto gen(F &&f, A... args) {
    return gen(std::make_index_sequence<N>{}, std::forward<F>(f), args...);
}

int f(int, char) { return 0; }

int main() {
    auto vec = gen<10>(&f, 0, 'c');
}

Return type of your function is easily deduced by: 您的函数的返回类型可以很容易地推导出:

decltype(std::forward<F>(f)(args...))

I want to create a container to contain n invocations of this function. 我想创建一个容器,以包含此函数的n个调用。 Then I want to execute it that many times. 然后,我要执行多次。

To do that, I used an std::index_sequence to create a parameter pack having the right size. 为此,我使用了std::index_sequence来创建具有正确大小的参数包。
Then the vector itself is initialized as it follows: 然后,向量本身将如下初始化:

(I, std::forward<F>(f)(args...))...

The basic idea is to exploit the comma operator to unpack the parameter pack mentioned above and execute N times the function. 基本思想是利用逗号运算符解压缩上述参数包并执行N次该函数。 The values returned by the invokations of f are used to fill the vector. f的调用返回的值用于填充向量。

Note that args are not perfectly forwarded to f . 请注意, args不能完美地转发给f
It could cause problems in case of moveable objects consumed during the first execution. 如果在第一次执行期间消耗了可移动对象,则可能导致问题。

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

相关问题 如何使用SFINAE为容器创建模板函数,并根据运算符推断出返回类型? - How can I use SFINAE to create a template function for containers, and deduce the return type based on an operator? 如何使用模板模板类型作为函数参数? - How can I use template template type as an function argument? 我如何找到模板函数返回的类型 - How I can find type of template function return 如何在模板参数中分离 function 类型返回类型和 arguments - How can I separate function type return type and arguments in template parameter 我可以默认使用此Transform模板函数的返回类型吗? - Can I default the return type of this Transform template function? 我可以避免返回函数的模板返回类型吗? - Can I avoid returning a template return type of a function? 在C ++模板函数中,我可以返回一个解除引用的参数类型吗? - In a C++ template function can I return a dereferenced argument type? 可以推导出作为模板Agument传递的Function的返回类型吗? - Can I Deduce the Return Type of a Function Passed as a Template Agument? 如何建立使用模板函数的 lambda function 的尾随返回类型? - How to establish trailing return type of lambda function that use template functions? 如何使用decltype作为模板化类的返回类型的模板参数? - How can I use decltype as the template parameter for a return type of a templated class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM