c++ - 使用 invoke_result 的正确方法?

标签 c++ c++17 forwarding-reference result-of

关于 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 typedef type names the type decltype(INVOKE(declval<Fn>(), declval<ArgTypes>()...)); otherwise, there shall be no member type.

declval 指定为:

template<class T> add_rvalue_reference_t<T> declval() noexcept;

所以 std::invoke_result_t<F&&, Args&&...> 之间没有区别和 std::invoke_result_t<F, Args...> .好吧,后者短了 4 个字符,但它们的意思完全相同(因为 FArgs... 都不可能是 void)。

关于c++ - 使用 invoke_result 的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47875365/

相关文章:

c++ - 使用 reverse_iterator 反转数组( vector )

c++ - 为什么调用具有通用或 r 值引用的重载覆盖函数是不明确的?

c++ - f() 按值返回对象的 "auto&& a= f();"中 a 的生命周期是多少?

c++ - 如何在 C++ 中将 uint64_t 转换为 const char *?

c++ - 汇编程序和 C++ 输出文本

c++ - 在返回类型上重载 operator[]

c++ - 有没有更好的替代 std::remove_if 从 vector 中删除元素的方法?

c++ - c++中使用引用重命名变量

c++ - 如何使用推导指南为 C++ 类模板提供两个兼容的名称?

c++ - 如何通过通用引用或 std::forward 将这三个 C++ 模板函数合并为一个?