Note that there are some explanatory texts on larger screens.

plurals
  1. POCan std::async be use with template functions
    primarykey
    data
    text
    <p>Is <a href="http://en.cppreference.com/w/cpp/thread/async"><code>std::async</code></a> supose to work with template function? I've tried to lauch <a href="http://en.cppreference.com/w/cpp/algorithm/reverse"><code>std::reverse</code></a> as an asynchronous task bu got compile-time error.</p> <p>I've tried to use simpler functions (foo and bar) and discover that only the non-template-function are working.</p> <pre><code>#include &lt;algorithm&gt; #include &lt;future&gt; #include &lt;string&gt; void foo(std::string::iterator first, std::string::iterator last) { } template&lt;class BidirectionalIterator&gt; 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(); } </code></pre> <p>The compiler error is as follow:</p> <pre><code>main.cpp: In function ‘int main()’: main.cpp:18:71: erreur: no matching function for call to ‘async(&lt;unresolved overloaded function type&gt;, std::basic_string&lt;char&gt;::iterator, std::basic_string&lt;char&gt;::iterator)’ main.cpp:18:71: note: candidates are: /usr/include/c++/4.6/future:1355:5: note: template&lt;class _Fn, class ... _Args&gt; std::future&lt;typename std::result_of&lt;_Functor(_ArgTypes ...)&gt;::type&gt; std::async(std::launch, _Fn&amp;&amp;, _Args&amp;&amp; ...) /usr/include/c++/4.6/future:1378:5: note: template&lt;class _Fn, class ... _Args&gt; typename std::__async_sfinae_helper&lt;typename std::decay&lt;_Functor&gt;::type, _Fn, _Args ...&gt;::type std::async(_Fn&amp;&amp;, _Args&amp;&amp; ...) main.cpp:18:71: erreur: unable to deduce ‘auto’ from ‘&lt;expression error&gt;’ main.cpp:20:62: erreur: no matching function for call to ‘async(&lt;unresolved overloaded function type&gt;, std::basic_string&lt;char&gt;::iterator, std::basic_string&lt;char&gt;::iterator)’ main.cpp:20:62: note: candidates are: /usr/include/c++/4.6/future:1355:5: note: template&lt;class _Fn, class ... _Args&gt; std::future&lt;typename std::result_of&lt;_Functor(_ArgTypes ...)&gt;::type&gt; std::async(std::launch, _Fn&amp;&amp;, _Args&amp;&amp; ...) /usr/include/c++/4.6/future:1378:5: note: template&lt;class _Fn, class ... _Args&gt; typename std::__async_sfinae_helper&lt;typename std::decay&lt;_Functor&gt;::type, _Fn, _Args ...&gt;::type std::async(_Fn&amp;&amp;, _Args&amp;&amp; ...) main.cpp:20:62: erreur: unable to deduce ‘auto’ from ‘&lt;expression error&gt;’ </code></pre> <p>However, it pass when I manually specify the template instanciation, such as <code>std::async(std::reverse&lt;std::string::iterator&gt;, str.begin(), str.end())</code>.</p> <p>Is this a compiler bug (GCC 4.6.3) or a well defined behaviour?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload