[英]Correct way of using invoke_result?
On cppreference , it is written that the correct way of using std::result_of
is: 在cppreference上 ,写道使用
std::result_of
的正确方法是:
template<class F, class... Args>
std::result_of_t<F&&(Args&&...)>
// instead of std::result_of_t<F(Args...)>, which is wrong
my_invoke(F&& f, Args&&... args) {
/* implementation */
}
I was wondering how std::invoke_result_t
should be used: invoke_result_t
: 我想知道应该如何使用
std::invoke_result_t
: invoke_result_t
:
template<class F, class... Args>
std::invoke_result_t<F&&, Args&&...> my_invoke(F&& f, Args&&... args);
Or: 要么:
template<class F, class... Args>
std::invoke_result_t<F, Args...> my_invoke(F&& f, Args&&... args);
invoke_result
is defined in terms of declval
: invoke_result
是根据declval
定义的:
If the expression
INVOKE(declval<Fn>(), declval<ArgTypes>()...)
is well-formed when treated as an unevaluated operand, the member typedeftype
names the typedecltype(INVOKE(declval<Fn>(), declval<ArgTypes>()...));
如果表达式
INVOKE(declval<Fn>(), declval<ArgTypes>()...)
在被视为未评估的操作数时格式正确,则成员typedeftype
将类型命名为decltype(INVOKE(declval<Fn>(), declval<ArgTypes>()...));
otherwise, there shall be no membertype
.否则,不得有会员
type
。
and declval
is specified as: 和
declval
指定为:
template<class T> add_rvalue_reference_t<T> declval() noexcept;
So there's no difference between std::invoke_result_t<F&&, Args&&...>
and std::invoke_result_t<F, Args...>
. 所以
std::invoke_result_t<F&&, Args&&...>
和std::invoke_result_t<F, Args...>
之间没有区别。 Well, the latter is 4 characters shorter, but they mean exactly the same thing (since neither F
nor Args...
could be void
). 嗯,后者短4个字符,但它们的意思完全相同(因为
F
和Args...
都不是void
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.