I have to call a function via a low level system routine, which gets the function and argument passed.
void doLater(void* func, void* arg);
I want to call the callLater function with any function and arguments using bind. So:
template<typename Call>
void caller(void *arg) {
std::unique_ptr<Call> upCall(static_cast<Call*>(arg));
(*upCall)();
}
template<class Function, class... Args>
void callLater(Function &&f, Args&&...args) {
typedef decltype(std::bind(std::forward<Function>(f), std::forward<Args>(args)...))) Call;
Call* call = new Call(std::bind(std::forward<Function>(f), std::forward<Args>(args)...));
doLater(caller<Call>, (void*) call);
}
Using this with std::unique_ptr leads into unwanted copies, so it cannot be compiled.
std::function<void(std::unique_ptr<int>)> foo = [](std::unique_ptr<int> i) {
std::cout << "Hello world!\n";
};
std::unique_ptr<int> value = std::make_unique<int>(1);
callLater(foo, std::move(value));
use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr...
One possible workaround is to use std::__bind_simple like it is used in std::thread (gcc) but it is not standardised. Any idea how to fix this problem?