简体   繁体   English

使用invoke_result的正确方法?

[英]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_tinvoke_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 typedef type names the type decltype(INVOKE(declval<Fn>(), declval<ArgTypes>()...)); 如果表达式INVOKE(declval<Fn>(), declval<ArgTypes>()...)在被视为未评估的操作数时格式正确,则成员typedef type将类型命名为decltype(INVOKE(declval<Fn>(), declval<ArgTypes>()...)); otherwise, there shall be no member type . 否则,不得有会员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个字符,但它们的意思完全相同(因为FArgs...都不是void )。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM