async如果第一个参数是指向成员函数的指针,第二个参数则提供了用来应用该成员函数的对象(直接地,或通过指针,或封装在 std::ref 中),其余的参数则作为参数传递给该成员函数。否则,第二个及后续的参数将作为参数,传递给第一个参数是所指定的函数或可调用对象。和 std::thread 一样,如果参数是右值,则通过移动原来的参数来创建副本。这就允许使用只可移动的类型同时作为函数对象和参数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   | #include <string> #include <future>
  struct X {     void foo(int, std::string const&);     std::string bar(string const&); } X x; auto f1 = std::async(&X::foo, &x, 42, "hello");  auto f2 = std::async(&X::bar, x, "goodbye"); 
  struct Y {     double operator()(double); } Y y; auto f3 = std::async(Y(), 3.141);  auto f4 = std::async(std::ref(y), 2.718); 
  X baz(X&); std::async(baz, std::ref(x)); 
  class move_only { public:     move_only();     move_only(move_only&&);     move_only(move_only const&) = delete;     move_only& operator=(move_only&&);     move_only& operator=(move_only const&) = delete;     void operator()(); } auto f5 = std::async(move_only()); 
   |