2013-02-16 22 views
5

के std :: async कॉल निम्नलिखित वर्ग पर विचार करें: bar को क्रियान्वित करनेसदस्य समारोह

class Foo 
{ 
    private: 
     void bar(const size_t); 
    public: 
     void foo(); 
}; 

धागे अब Foo::foo() शुरू कर देना चाहिए, तो यह है कि यह कैसे लागू किया गया है:

void Foo:foo() 
{ 
    auto handle = std::async(std::launch::async, &Foo::bar, this, 0); 
    handle.get(); 
} 

इस ग्राम के साथ दोषरहित काम करता है ++ -4.6.3, लेकिन जी ++ के साथ नहीं - 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, this, 0); 
    handle.get(); 
} 

हालांकि, एक विधि को सार्वजनिक करने के लिए सबसे अच्छा डिजाइन निर्णय नहीं है। बिना संकलक को बदलने और विधि को निजी छोड़कर इस मुद्दे के आसपास काम करने का कोई और तरीका है?

उत्तर

9

समस्या यह प्रतीत होती है कि यह सदस्य कार्यों के साथ अच्छा नहीं खेलेंगे। शायद आप पहली बार अपने वस्तु को सदस्य समारोह std::bind सकते हैं, यह std::async को पार करने से पहले:

auto func = std::bind(&Foo::bar, this, std::placeholders::_1); 
auto handle = std::async(std::launch::async, func, 0); 
+0

ठीक काम करता है, धन्यवाद! – stefan

संबंधित मुद्दे