小编mau*_*uve的帖子

将带有回调的函数调用转换为协程

我正在探索并尝试学习 C++ 协程(在 C++20 中添加)。我正在使用的 SDK 具有异步 API 调用,这些调用都采用回调,回调是在 SDK 管理的某些后台线程上调用的。


namespace third_party {

bool api_call(const std::string& some_parameter, const std::function<void(std::error_code)>& callback);

} // namespace third_party

Run Code Online (Sandbox Code Playgroud)

我想将这个 API 调用包装成可以等待的东西:


namespace my_third_party_sdk_wrapper {

cppcoro::task<std::error_code> api_call(const std::string& some_parameter);
cppcoro::task<std::error_code> api_call(const std::string& some_parameter, cppcoro::cancellation_token token);

} // namespace my_third_party_sdk_wrapper 

Run Code Online (Sandbox Code Playgroud)

我正在考虑使用cppcoro lib,但这不是必需的,除非包装器的实现通过这样做变得更加简单。

问题是我无法弄清楚如何实现包装器。

c++ c++20 c++-coroutine

6
推荐指数
1
解决办法
2259
查看次数

使用std :: result_of <F>时,模板推断失败

我正在尝试创建一个函数,该函数调用并返回作为模板参数传递的函数的返回值:

template <typename Function>
typename std::result_of<Function>::type
call_function(Function&& f)
{
  return f();
}

template <typename Function, typename Class>
typename std::result_of<Function>::type
call_member_function(Function&& f, Class* instance)
{
  return instance->*f();
}

//
// call site:
//

call_function(f);
call_member_function(&F::f, &instance); 
Run Code Online (Sandbox Code Playgroud)

这是一个ideone版本:http://ideone.com/IYM10x (它在VS2013.4中以类似的方式失败)

我已经将论证替换std::result_of为不同的排列std::decay,std::remove_reference并且std::remove_pointer没有任何运气.

我如何制作call_functioncall_member_function编译,最好还为返回的函数void

c++ templates type-deduction

1
推荐指数
1
解决办法
714
查看次数

标签 统计

c++ ×2

c++-coroutine ×1

c++20 ×1

templates ×1

type-deduction ×1