>  Q&A  >  본문

参数中有回调函数的异步函数在C++11中一般如何定义?

我希望传入的回调函数既可以是传统的函数指针,也可以传入C++新标准中的匿名函数、std::function,那么我应该如何定义这个异步函数的原型呢?难道必须要分开分别定义吗?
C++编写异步函数的最佳实践一般是怎样的呢?

PHP中文网PHP中文网2715일 전672

모든 응답(1)나는 대답할 것이다

  • 伊谢尔伦

    伊谢尔伦2017-04-17 11:46:00

    // Function with any callable type as callback
    template<typename Callable, typename ...Args>
    void func_with_callback(int foo, int bar, Callable&& f, Args&&... args) {
        // Do something
        // ...
        // Invoke callback
        std::forward<Callable>(f)(std::forward<Args>(args)...);
    }
    

    회신하다
    0
  • 취소회신하다