关于 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 */
}
我想知道应该如何使用 std::invoke_result_t
:
调用结果
:
template<class F, class... Args>
std::invoke_result_t<F&&, Args&&...> my_invoke(F&& f, Args&&... args);
或者:
template<class F, class... Args>
std::invoke_result_t<F, Args...> my_invoke(F&& f, Args&&... args);
最佳答案
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>()...));
otherwise, there shall be no membertype
.
和 declval
指定为:
template<class T> add_rvalue_reference_t<T> declval() noexcept;
所以 std::invoke_result_t<F&&, Args&&...>
之间没有区别和 std::invoke_result_t<F, Args...>
.好吧,后者短了 4 个字符,但它们的意思完全相同(因为 F
和 Args...
都不可能是 void
)。
关于c++ - 使用 invoke_result 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47875365/