std::forword 用来转发函数调用的参数,按原样转发。
模板加std::forword为泛型编程提供了很大的方便,通过模板加std::forword实现一个通用的,可接收不同参数的统一方法调用。
- #include
- #include
- #include
- #include
- #include
-
- template<typename F, typename ... Types>
- void test_forword(F f, Types ... args)
- {
- auto func = std::bind(std::forward
(f), std::forward(args)...); - func();
-
- /*using ret_type = typename std::result_of
::type; - std::packaged_task
task(func); - std::future
fret = task.get_future(); - std::thread t4(std::move(task));
- int ret = fret.get();
- std::cout << "forword bind task ret:" << ret << std::endl;
- t4.join();*/
- }
-
- int forword_task(int a, int b)
- {
- std::cout << "forword_task a:" << a << std::endl;
- std::cout << "forword_task b:" << b << std::endl;
- return a + b;
- }
-
- class Forward
- {
- public:
- int forword_task(int a, int b)
- {
- std::cout << "forword_task a:" << a << std::endl;
- std::cout << "forword_task b:" << b << std::endl;
- return a + b;
- }
- private:
- int c_ = 0;
- };
-
-
-
- int main()
- {
- test_forword(forword_task, 10, 20);
-
- Forward fw;
- test_forword(&Forward::forword_task, &fw, 30, 40);
- return 0;
-
- }