How do I get boost::async to pass a function argument to the function it executes. My code is
#include <iostream>
// This is for the boos version
#include <boost/thread/future.hpp>
using boost::async;
// This is for the standard library version
//~ #include <future>
//~ using std::async;
int f(int x) {
std::cout << " in f " << x << std::endl;
return x+1;
}
int main() {
auto f_res = async(f,3);
std::cout << "f " << f_res.get() << std::endl;
return 0;
}
I compile with
g++ -std=c++14 -lboost_thread -lboost_system -lpthread test_async_boost.cc
, g++ version 5.3.0 and get many errors, complaining about the number of arguments async is willing to take:
test_async_boost_2.cc: In function 'int main()':
test_async_boost_2.cc:16:26: error: no matching function for call to 'async(int (&)(int), int)'
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:4035:3: note: candidate: template<class F> boost::unique_future<typename boost::result_of<F()>::type> boost::async(F&&)
async(BOOST_THREAD_FWD_REF(F) f) {
^
/usr/include/boost/thread/future.hpp:4035:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: candidate expects 1 argument, 2 provided
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:4018:3: note: candidate: template<class R> boost::unique_future<T> boost::async(R (*)())
async(R(*f)()) {
^
/usr/include/boost/thread/future.hpp:4018:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: candidate expects 0 arguments, 1 provided
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:3695:3: note: candidate: template<class F> boost::unique_future<typename boost::result_of<typename boost::decay<T>::type()>::type> boost::async(boost::launch, F&&)
async(launch policy, BOOST_THREAD_FWD_REF(F) f) {
^
/usr/include/boost/thread/future.hpp:3695:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: cannot convert 'f' (type 'int(int)') to type 'boost::launch'
auto f_res = async(f,3);
^
In file included from test_async_boost_2.cc:3:0:
/usr/include/boost/thread/future.hpp:3634:3: note: candidate: template<class R> boost::unique_future<T> boost::async(boost::launch, R (*)())
async(launch policy, R(*f)()) {
^
/usr/include/boost/thread/future.hpp:3634:3: note: template argument deduction/substitution failed:
test_async_boost_2.cc:16:26: note: cannot convert 'f' (type 'int(int)') to type 'boost::launch'
auto f_res = async(f,3);
^
If I switch the commenting in the #include directuves and the using lines, and compile with
g++ -std=c++14 test_async_boost_2.cc -lpthread
I get the desired output:
in f 3
f 4
How do I get boost::async to work with function arguments?
And: Where do I find the reference documentation of boos::async?
In order to pass a function with its arguments to the method accepting a function, you have to use std::bind or boost::bind.
Related
I try to implement a class which accepts a function and a variadic argument list as input to execute these functions later on a worker thread. My current implementation has a problem, if one of the arguments is a reference.
Have a look at the following smaller code example:
#include <functional>
#include <iostream>
template<typename Result, typename ...Args>
Result Foo(Result f(Args...), Args... args)
{
return f(args...);
}
int BarValue(int x){return x;}
int BarPointer(int* x){ *x++;return *x; }
int BarRef(int& x){ x++; return x; }
int main()
{
int x{0};
std::cout << Foo(BarValue, x) << std::endl;
std::cout << Foo(BarPointer, &x) << std::endl;
std::cout << Foo(BarRef, x) << std::endl; // does not compile: Error 1
std::cout << Foo(BarRef, std::ref(x)) << std::endl; // does also not compile: Error 2
return 0;
}
Error 1:
<source>: In function 'int main()':
<source>:31:31: error: no matching function for call to 'Foo(int (&)(int&), int&)'
31 | std::cout << Foo(BarRef, x) << std::endl;
| ^
<source>:4:8: note: candidate: 'template<class Result, class ... Args> Result Foo(Result (*)(Args ...), Args ...)'
4 | Result Foo(Result f(Args...), Args... args)
| ^~~
<source>:4:8: note: template argument deduction/substitution failed:
<source>:31:31: note: inconsistent parameter pack deduction with 'int&' and 'int'
31 | std::cout << Foo(BarRef, x) << std::endl;
| ^
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:31:31: error: no matching function for call to 'Foo(int (&)(int&), int&)'
31 | std::cout << Foo(BarRef, x) << std::endl;
| ^
<source>:4:8: note: candidate: 'template<class Result, class ... Args> Result Foo(Result (*)(Args ...), Args ...)'
4 | Result Foo(Result f(Args...), Args... args)
| ^~~
<source>:4:8: note: template argument deduction/substitution failed:
<source>:31:31: note: inconsistent parameter pack deduction with 'int&' and 'int'
31 | std::cout << Foo(BarRef, x) << std::endl;
|
^
Error 2:
<source>: In function 'int main()':
<source>:33:41: error: no matching function for call to 'Foo(int (&)(int&), std::reference_wrapper<int>)'
33 | std::cout << Foo(BarRef, std::ref(x)) << std::endl;
| ^
<source>:5:8: note: candidate: 'template<class Result, class ... Args> Result Foo(Result (*)(Args ...), Args ...)'
5 | Result Foo(Result f(Args...), Args... args)
| ^~~
<source>:5:8: note: template argument deduction/substitution failed:
<source>:33:41: note: inconsistent parameter pack deduction with 'int&' and 'std::reference_wrapper<int>'
33 | std::cout << Foo(BarRef, std::ref(x)) << std::endl;
| ^
ASM generation compiler returned: 1
<source>: In function 'int main()':
<source>:33:41: error: no matching function for call to 'Foo(int (&)(int&), std::reference_wrapper<int>)'
33 | std::cout << Foo(BarRef, std::ref(x)) << std::endl;
| ^
<source>:5:8: note: candidate: 'template<class Result, class ... Args> Result Foo(Result (*)(Args ...), Args ...)'
5 | Result Foo(Result f(Args...), Args... args)
| ^~~
<source>:5:8: note: template argument deduction/substitution failed:
<source>:33:41: note: inconsistent parameter pack deduction with 'int&' and 'std::reference_wrapper<int>'
33 | std::cout << Foo(BarRef, std::ref(x)) << std::endl;
| ^
Execution build compiler returned: 1
Compiled with gcc 10.2 and -O3 -std=c++17 : GodBolt
How can I solve this reference problem?
My recommendation is that you take a look at how the standard library itself uses templates to pass callable objects (functions, lambdas, etc.): By using a single template type:
template<typename Func, typename ...Args>
auto Foo(Func f, Args&&... args)
{
return f(std::forward<Args>(args)...);
}
Note that I have added a call to std::forward to properly "forward" the arguments.
Also note that I made the return-type auto to let the compiler deduce the return type.
If I understand your comments correctly, you want to create a variable that holds the returned value of f, and then return that variable later? Then you could either do it using decltype as you do in your compiler-explorer link, or just use plain auto again when defining and initializing your variable:
template<typename Func, typename ...Args>
auto Foo(Func f, Args&&... args)
{
auto value = f(std::forward<Args>(args)...);
// Do something with the variable value...
return value;
}
This will of course not work if the function f have a void return value.
The following code for a simple hash function won't compile
#include <cstddef>
#include <functional>
namespace {
struct Foo {
long i;
};
}
namespace std {
template<> struct hash<::Foo> {
size_t operator()(::Foo foo) const {
return hash<decltype(foo.i)>(foo.i);
}
};
}
My 4.8.5 g++ compiler emits these messages:
$ g++ -std=c++11 a.cpp
a.cpp: In member function ‘std::size_t std::hash<{anonymous}::Foo>::operator()({anonymous}::Foo) const’:
a.cpp:13:47: error: no matching function for call to ‘std::hash<long int>::hash(long int&)’
return hash<decltype(foo.i)>(foo.i);
^
a.cpp:13:47: note: candidates are:
In file included from /usr/include/c++/4.8.2/bits/basic_string.h:3033:0,
from /usr/include/c++/4.8.2/string:52,
from /usr/include/c++/4.8.2/stdexcept:39,
from /usr/include/c++/4.8.2/array:38,
from /usr/include/c++/4.8.2/tuple:39,
from /usr/include/c++/4.8.2/functional:55,
from a.cpp:2:
/usr/include/c++/4.8.2/bits/functional_hash.h:107:3: note: constexpr std::hash<long int>::hash()
_Cxx_hashtable_define_trivial_hash(long)
^
/usr/include/c++/4.8.2/bits/functional_hash.h:107:3: note: candidate expects 0 arguments, 1 provided
/usr/include/c++/4.8.2/bits/functional_hash.h:107:3: note: constexpr std::hash<long int>::hash(const std::hash<long int>&)
/usr/include/c++/4.8.2/bits/functional_hash.h:107:3: note: no known conversion for argument 1 from ‘long int’ to ‘const std::hash<long int>&’
/usr/include/c++/4.8.2/bits/functional_hash.h:107:3: note: constexpr std::hash<long int>::hash(std::hash<long int>&&)
/usr/include/c++/4.8.2/bits/functional_hash.h:107:3: note: no known conversion for argument 1 from ‘long int’ to ‘std::hash<long int>&&’
$ fg
The problem appears to be the call-by-reference in the first error message but I don't understand why or how to fix it.
You are missing a set of parentheses in
return hash<decltype(foo.i)>(foo.i);
In the above you are trying to construct a std::hash, not call its operator(). You need
return hash<decltype(foo.i)>()(foo.i);
// or
return hash<decltype(foo.i)>{}(foo.i);
where the empty set of parentheses/curly braces constructs the hash object, and the second set calls its operator()
I tried to compile and run example of reset() function from http://www.cplusplus.com/reference/future/packaged_task/reset/:
$ cat task.cpp
// packaged_task::get_future
#include <iostream> // std::cout
#include <utility> // std::move
#include <future> // std::packaged_task, std::future
#include <thread> // std::thread
// a simple task:
int triple (int x) { return x*3; }
int main ()
{
std::packaged_task<int(int)> tsk (triple); // package task
std::future<int> fut = tsk.get_future();
std::thread (std::move(tsk),33).detach();
std::cout << "The triple of 33 is " << fut.get() << ".\n";
// re-use same task object:
tsk.reset();
fut = tsk.get_future();
std::thread(std::move(tsk),99).detach();
std::cout << "Thre triple of 99 is " << fut.get() << ".\n";
return 0;
}
but I got either compiler error or runtime error, depending on compiler used:
GCC:
$ g++ -o task -std=c++11 -I/opt/local/include task.cpp && ./task
The triple of 33 is 99.
terminate called after throwing an instance of 'std::future_error'
what(): No associated state
[1] 14850 abort ./task
CLANG:
$ clang++ -o task -std=c++11 -stdlib=libc++ -I/opt/local/include task.cpp && ./task
In file included from task.cpp:2:
In file included from /opt/local/libexec/llvm-3.4/bin/../include/c++/v1/iostream:38:
In file included from /opt/local/libexec/llvm-3.4/bin/../include/c++/v1/ios:216:
In file included from /opt/local/libexec/llvm-3.4/bin/../include/c++/v1/__locale:15:
In file included from /opt/local/libexec/llvm-3.4/bin/../include/c++/v1/string:439:
In file included from /opt/local/libexec/llvm-3.4/bin/../include/c++/v1/algorithm:627:
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/memory:1627:45: error: multiple overloads of 'address' instantiate to the same
signature 'const_pointer (const_reference) const noexcept'
_LIBCPP_INLINE_VISIBILITY const_pointer address(const_reference __x) const _NOEXCEPT
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/type_traits:922:38: note: in instantiation of template class
'std::__1::allocator<int (int)>' requested here
: public integral_constant<bool, __is_empty(_Tp)> {};
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/memory:1908:40: note: in instantiation of template class
'std::__1::is_empty<std::__1::allocator<int (int)> >' requested here
bool = is_empty<_T2>::value
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/memory:1930:44: note: in instantiation of default argument for
'__libcpp_compressed_pair_switch<int (int), std::__1::allocator<int (int)>, false, false>' required here
template <class _T1, class _T2, unsigned = __libcpp_compressed_pair_switch<_T1, _T2>::value>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/memory:2298:15: note: in instantiation of default argument for
'__libcpp_compressed_pair_imp<int (int), std::__1::allocator<int (int)> >' required here
: private __libcpp_compressed_pair_imp<_T1, _T2>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/future:1770:36: note: in instantiation of template class
'std::__1::__compressed_pair<int (int), std::__1::allocator<int (int)> >' requested here
__compressed_pair<_Fp, _Alloc> __f_;
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/future:1877:9: note: in instantiation of template class
'std::__1::__packaged_task_func<int (int), std::__1::allocator<int (int)>, int (int)>' requested here
if (sizeof(_FF) <= sizeof(__buf_))
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/future:2026:45: note: in instantiation of function template specialization
'std::__1::__packaged_task_function<int (int)>::__packaged_task_function<int (&)(int)>' requested here
explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
^
task.cpp:12:32: note: in instantiation of function template specialization 'std::__1::packaged_task<int
(int)>::packaged_task<int (&)(int), void>' requested here
std::packaged_task<int(int)> tsk (triple); // package task
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/memory:1625:39: note: previous declaration is here
_LIBCPP_INLINE_VISIBILITY pointer address(reference __x) const _NOEXCEPT
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/memory:2121:9: error: data member instantiated with function type 'int (int)'
_T1 __first_;
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/memory:2298:15: note: in instantiation of template class
'std::__1::__libcpp_compressed_pair_imp<int (int), std::__1::allocator<int (int)>, 2>' requested here
: private __libcpp_compressed_pair_imp<_T1, _T2>
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/future:1770:36: note: in instantiation of template class
'std::__1::__compressed_pair<int (int), std::__1::allocator<int (int)> >' requested here
__compressed_pair<_Fp, _Alloc> __f_;
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/future:1877:9: note: in instantiation of template class
'std::__1::__packaged_task_func<int (int), std::__1::allocator<int (int)>, int (int)>' requested here
if (sizeof(_FF) <= sizeof(__buf_))
^
/opt/local/libexec/llvm-3.4/bin/../include/c++/v1/future:2026:45: note: in instantiation of function template specialization
'std::__1::__packaged_task_function<int (int)>::__packaged_task_function<int (&)(int)>' requested here
explicit packaged_task(_Fp&& __f) : __f_(_VSTD::forward<_Fp>(__f)) {}
^
task.cpp:12:32: note: in instantiation of function template specialization 'std::__1::packaged_task<int
(int)>::packaged_task<int (&)(int), void>' requested here
std::packaged_task<int(int)> tsk (triple); // package task
^
2 errors generated.
I'm using OSX Maverics and compilers:
$ g++ --version
g++ (MacPorts gcc48 4.8.2_0) 4.8.2
$ clang++ --version
clang version 3.4 (tags/RELEASE_34/final)
Target: x86_64-apple-darwin13.1.0
Thread model: posix
Could someone please help me?
GCC is correct:
std::thread (std::move(tsk),33).detach(); // 1
//...
tsk.reset(); // 2
After (1), tsk has no more state (per move constructor of packaged_task)
At (2), reset() is required to throw the no_state std::future_error
clang had a bug, as pointed out in the comments.
I have a question about the Functor implementation of the library Loki.
I am doing some changes in order to make it work with variadic templates instead of having lines and lines of template specialization. The problem is that I am trying to use typedef for variadic template and I do not understand my error, that is why I could use some help from experts...
Here is the header file.
I tested it with a simple example:
static void foo()
{
std::cout << "foo !!!" << std::endl;
}
int
main( int argc, const char** argv )
{
Functor<void, void> static_func(foo);
static_func();
}
Which gives me this error
/home/test/src/EntryPoint.cpp:237:17: error: no match for call to ‘(Functor<void, void>) ()’
In file included from /home/test/src/EntryPoint.cpp:231:0:
/home/test/include/FunctorTest.h:217:7: note: candidate is:
/home/test/include/FunctorTest.h:292:16: note: Functor<R, TList>::ResultType Functor<R, TList>::operator()(Functor<R, TList>::MyList&&) const [with R = void; TList = {void}; Functor<R, TList>::ResultType = void; Functor<R, TList>::MyList = variadic_typedef<void>]
/home/test/include/FunctorTest.h:292:16: note: candidate expects 1 argument, 0 provided
/home/test/src/EntryPoint.cpp: At global scope:
/home/test/src/EntryPoint.cpp:234:1: warning: unused parameter ‘argc’ [-Wunused-parameter]
/home/test/src/EntryPoint.cpp:234:1: warning: unused parameter ‘argv’ [-Wunused-parameter]
In file included from /home/test/src/EntryPoint.cpp:231:0:
/home/test/include/FunctorTest.h: In instantiation of ‘FunctorHandler<ParentFunctor, Fun>::ResultType FunctorHandler<ParentFunctor, Fun>::operator()(FunctorHandler<ParentFunctor, Fun>::MyList&&) [with ParentFunctor = Functor<void, void>; Fun = void (*)(); FunctorHandler<ParentFunctor, Fun>::ResultType = void; FunctorHandler<ParentFunctor, Fun>::MyList = variadic_typedef<void>]’:
/home/test/src/EntryPoint.cpp:247:1: required from here
/home/test/include/FunctorTest.h:159:49: error: no matching function for call to ‘forward(FunctorHandler<Functor<void, void>, void (*)()>::MyList&)’
/home/test/include/FunctorTest.h:159:49: note: candidates are:
In file included from /usr/include/c++/4.7/bits/stl_pair.h:61:0,
from /usr/include/c++/4.7/utility:72,
from /home/jean/Lib/vitals/include/CLPair.h:28,
from /home/jean/Lib/vitals/include/CLMap.h:27,
from /home/jean/Lib/vitals/include/HTCmdLineParser.h:27,
from /home/test/include/EntryPoint.h:23,
from /home/test/src/EntryPoint.cpp:22:
/usr/include/c++/4.7/bits/move.h:77:5: note: template<class _Tp> constexpr _Tp&& std::forward(typename std::remove_reference<_From>::type&)
/usr/include/c++/4.7/bits/move.h:77:5: note: template argument deduction/substitution failed:
In file included from /home/test/src/EntryPoint.cpp:231:0:
/home/test/include/FunctorTest.h:159:49: note: cannot convert ‘parms’ (type ‘FunctorHandler<Functor<void, void>, void (*)()>::MyList {aka variadic_typedef<void>}’) to type ‘std::remove_reference<convert_in_tuple<variadic_typedef<void> > >::type& {aka convert_in_tuple<variadic_typedef<void> >&}’
Okay I've developed some code;
Linkage (it's rather long)
And when I compile it with Clang++ 3.2, it runs and results in;
stdout:
print: C-3PO
print: R2D2
However if I try to compile it with G++ 4.7.2, I get these errors;
Compilation finished with errors:
source.cpp: In function 'int main()':
source.cpp:90:71: error: no matching function for call to 'makeRunnable(int (&)(char, int, const char*), char, int)'
source.cpp:90:71: note: candidate is:
source.cpp:74:27: note: template<class ... RUN_TIME, class T, class ... CONSTRUCTION_TIME> Runnable<T, RUN_TIME ...>* makeRunnable(T (*)(CONSTRUCTION_TIME ..., RUN_TIME ...), CONSTRUCTION_TIME ...)
source.cpp:74:27: note: template argument deduction/substitution failed:
source.cpp:90:71: note: mismatched types 'const char*' and 'char'
source.cpp:90:71: error: unable to deduce 'auto' from '<expression error>'
source.cpp:92:72: error: no matching function for call to 'makeRunnable(int (&)(char, int, const char*), char)'
source.cpp:92:72: note: candidate is:
source.cpp:74:27: note: template<class ... RUN_TIME, class T, class ... CONSTRUCTION_TIME> Runnable<T, RUN_TIME ...>* makeRunnable(T (*)(CONSTRUCTION_TIME ..., RUN_TIME ...), CONSTRUCTION_TIME ...)
source.cpp:74:27: note: template argument deduction/substitution failed:
source.cpp:92:72: note: mismatched types 'int' and 'char'
source.cpp:92:72: error: unable to deduce 'auto' from '<expression error>'
And almost the same with G++ 4.8.0 (although prettier formatted).
So the question is;
Is this code standard conformant? - If not why?
Edit Relevant code from link:
template<typename... RUN_TIME, typename T, typename... CONSTRUCTION_TIME>
Runnable<T, RUN_TIME...>* makeRunnable(T (*FunctionType)(CONSTRUCTION_TIME..., RUN_TIME...), CONSTRUCTION_TIME... ct_args) // Line 74
{
return new FunctionDelegate<T,
std::tuple<CONSTRUCTION_TIME...>,
std::tuple<CONSTRUCTION_TIME..., RUN_TIME...>,
RUN_TIME...>(FunctionType, std::make_tuple(ct_args...));
}
int print_function(char arg1, int arg2, const char* arg3)
{
std::cout << "print: " << arg1 << arg2 << arg3 << std::endl;
return 2;
}
int main()
{
auto function1 = makeRunnable<const char*>(print_function, 'C', -3); // Line 90
int n = function1->invoke("PO");
auto function2 = makeRunnable<int, const char*>(print_function, 'R'); // Line 92
function2->invoke(n, "D2");
}
The point of this question, isn't really the implementation in question, more that Clang++ and G++ is not disagreeing on whether this is an error or not.
Having played with the code a bit, it seems that g++ can't handle the deducing of variadic template concatenation (between the |!|).
Runnable<T, RUN_TIME...>* makeRunnable(T (*FunctionType)|!|(CONSTRUCTION_TIME..., RUN_TIME...)|!|, CONSTRUCTION_TIME... ct_args)
And the code is fixable, by adding another variadic template argument, such that g++ can deduce the template directly.