std::async
टेम्पलेट फ़ंक्शन के साथ काम करने का अनुमान है? मैंने std::reverse
को लॉन्च करने का प्रयास किया है क्योंकि एसिंक्रोनस टास्क bu को संकलन-समय त्रुटि मिली है।क्या std :: async टेम्पलेट फ़ंक्शंस के साथ उपयोग किया जा सकता है
मैंने सरल कार्यों (foo और bar) का उपयोग करने की कोशिश की है और पता चलता है कि केवल गैर-टेम्पलेट-फ़ंक्शन काम कर रहा है।
#include <algorithm>
#include <future>
#include <string>
void foo(std::string::iterator first, std::string::iterator last)
{
}
template<class BidirectionalIterator>
void bar(BidirectionalIterator first, BidirectionalIterator last)
{
}
int main()
{
std::string str = "Lorem ipsum, dolor sit amet";
auto result_reverse = std::async(std::reverse, str.begin(), str.end()); // Compile-time error
auto result_foo = std::async(foo, str.begin(), str.end());
auto result_bar = std::async(bar, str.begin(), str.end()); // Compile-time error
result_reverse.get();
result_foo.get();
result_bar.get();
}
संकलक त्रुटि के रूप में अनुवर्ती है:
main.cpp: In function ‘int main()’:
main.cpp:18:71: erreur: no matching function for call to ‘async(<unresolved overloaded function type>, std::basic_string<char>::iterator, std::basic_string<char>::iterator)’
main.cpp:18:71: note: candidates are:
/usr/include/c++/4.6/future:1355:5: note: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
/usr/include/c++/4.6/future:1378:5: note: template<class _Fn, class ... _Args> typename std::__async_sfinae_helper<typename std::decay<_Functor>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...)
main.cpp:18:71: erreur: unable to deduce ‘auto’ from ‘<expression error>’
main.cpp:20:62: erreur: no matching function for call to ‘async(<unresolved overloaded function type>, std::basic_string<char>::iterator, std::basic_string<char>::iterator)’
main.cpp:20:62: note: candidates are:
/usr/include/c++/4.6/future:1355:5: note: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
/usr/include/c++/4.6/future:1378:5: note: template<class _Fn, class ... _Args> typename std::__async_sfinae_helper<typename std::decay<_Functor>::type, _Fn, _Args ...>::type std::async(_Fn&&, _Args&& ...)
main.cpp:20:62: erreur: unable to deduce ‘auto’ from ‘<expression error>’
हालांकि, इसे पारित जब मैं मैन्युअल std::async(std::reverse<std::string::iterator>, str.begin(), str.end())
के रूप में, टेम्पलेट instanciation निर्दिष्ट इस तरह के।
क्या यह एक कंपाइलर बग (जीसीसी 4.6.3) या एक अच्छी तरह से परिभाषित व्यवहार है?
टेम्पलेट्स कार्यों नहीं हैं, इसलिए व्यवहार मानक के अनुसार सही है । यदि आप तर्कों को कम करना चाहते हैं, तो आपको इसे एक मजेदार में लपेटना होगा। – Xeo