考虑以下课程:
class Foo { private: void bar(const size_t); public: void foo(); };
现在Foo :: foo()应该启动执行bar的线程,所以这就是它的实现方式:
void Foo:foo() { auto handle = std::async(std::launch::async,&Foo::bar,this,0); handle.get(); }
这与g -4.6.3完美配合,但不是g -4.5.2,错误信息是
include/c++/4.5.2/functional:180:9: Error: must use ».« or »->« to call pointer-to-member function in »std::declval with _Tp = void (Foo::*)(long unsigned int),typename std::add_rvalue_reference<_Tp>::type = void (Foo::&&)(long unsigned int) (…)«,e.g. »(… -> std::declval with _Tp = void (Foo::*)(long unsigned int),typename std::add_rvalue_reference<_Tp>::type = void (Foo::*&&)(long unsigned int)) (…)«
显然,错误在于旧版本的g.通过将方法公开并引入以下帮助函数,可以解决此问题:
void barHelp(Foo* foo,const size_t n) { foo->bar(n); } void Foo:foo() { auto handle = std::async(std::launch::async,barHelp,0); handle.get(); }