C++ async with variadic template can not find corr

2019-08-06 12:34发布

I have a class with a member function f, I wrap it by variadic template and forward to make another member function rf (just add a specific parameter at the end of f to do a little bit different thing). Then, I make another member function async_rf by wrapping rf with async, but it doesn't work. I try to make async_rf by wrapping f with additional specific parameter, and it works.

code :

#include <future>         // std::async, std::future
#include <iostream>

class test {

public:

    void f(int tmp, bool reverse = 0)
    {
        std::cout << tmp << " | " << reverse << std::endl;
    }

    template<typename... Args>
    void rf(Args... args)
    {
        f(std::forward<Args>(args)..., 1);
    }

    template<typename... Args>
    std::future<void> async_rf(Args... args)
    {
        // doesn't work
        return std::async (&test::rf, this, std::forward<Args>(args)...);

        // work
        return std::async (&test::f, this, std::forward<Args>(args)..., 1);
    }

};


int main()
{
    test s;
    auto tmp = s.async_rf(10);
    tmp.get();
    return 0;
}

here is the error message at compile time :

(clang)

$ clang++ --version
clang version 3.6.1 (tags/RELEASE_361/final)
Target: x86_64-unknown-linux-gnu
Thread model: posix
$ clang++ -std=c++14 -Wall -lpthread src/test.cpp -o bin/test
src/test.cpp:23:16: error: no matching function for call to 'async'
        return std::async (&test::rf, this, std::forward<Args>(args)...);
               ^~~~~~~~~~
src/test.cpp:35:18: note: in instantiation of function template specialization 'test::async_rf<int>' requested here
    auto tmp = s.async_rf(10);
                 ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../include/c++/5.1.0/future:1723:5: note: candidate template
      ignored: couldn't infer template argument '_Fn'
    async(_Fn&& __fn, _Args&&... __args)
    ^
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/5.1.0/../../../../include/c++/5.1.0/future:1703:5: note: candidate template
      ignored: substitution failure [with _Fn = test *, _Args = <int>]: no type named 'type' in
      'std::result_of<test *(int)>'
    async(launch __policy, _Fn&& __fn, _Args&&... __args)
    ^
1 error generated.

(gcc)

$ g++ --version
g++ (GCC) 5.1.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ g++ -std=c++14 -Wall -lpthread src/test.cpp -o bin/test
src/test.cpp: In instantiation of ‘std::future<void> test::async_rf(Args ...) [with Args = {int}]’:
src/test.cpp:35:29:   required from here
src/test.cpp:23:27: error: no matching function for call to ‘async(<unresolved overloaded function type>, test*, int)’
         return std::async (&test::rf, this, std::forward<Args>(args)...);
                           ^
In file included from src/test.cpp:1:0:
/usr/include/c++/5.1.0/future:1703:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...)
     async(launch __policy, _Fn&& __fn, _Args&&... __args)
     ^
/usr/include/c++/5.1.0/future:1703:5: note:   template argument deduction/substitution failed:
src/test.cpp:23:27: note:   cannot convert ‘&((test*)this)->*test::rf’ (type ‘<unresolved overloaded function type>’) to type ‘std::launch’
         return std::async (&test::rf, this, std::forward<Args>(args)...);
                           ^
In file included from src/test.cpp:1:0:
/usr/include/c++/5.1.0/future:1723:5: note: candidate: template<class _Fn, class ... _Args> std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(_Fn&&, _Args&& ...)
     async(_Fn&& __fn, _Args&&... __args)
     ^
/usr/include/c++/5.1.0/future:1723:5: note:   template argument deduction/substitution failed:
src/test.cpp:23:27: note:   couldn't deduce template parameter ‘_Fn’
         return std::async (&test::rf, this, std::forward<Args>(args)...);
                           ^

Does anyone can provide more detail about why it doesn't work ? Why compiler can not find correct template specialization ?

2条回答
做个烂人
2楼-- · 2019-08-06 12:46

Requiring the compiler to deduce specialization of rf by arguments provided to async would require it to look into the implementation of async which is a bit too much.

Just specify the template parameters yourself:

return std::async (&test::rf<Args...>, this, std::forward<Args>(args)...);

BTW you may want to change Args... to Args&&... everywhere, otherwise the arguments are passed by value.

查看更多
再贱就再见
3楼-- · 2019-08-06 12:48

Since rf is a function template, you can't simply pass it to std::async. First, you have to specify its template parameters, which is simply Args.... So your function would look like:

template<typename... Args>
std::future<void> async_rf(Args... args)
{
    // doesn't work
    return std::async (&test::rf<Args...>,this,std::forward<Args>(args)...);

    // work
    return std::async (&test::f, this, std::forward<Args>(args)..., 1);
}

Although having 2 return statements in that function probably isn't what you want to do.

查看更多
登录 后发表回答