[英]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.