I am trying to wrap the passed function inside a try-catch clause so that I can catch the exceptions thrown by it and do some cleanup before re-throwing. I wrote a sample code that replicates my compilation error.
#include <functional>
#include <iostream>
#include <queue>
#include <string.h>
#include <stdexcept>
using namespace std;
void foo(int a){
throw runtime_error("died");
}
template<class F,class ...Args>
void gen(F&& f,Args&&... args){
auto wrap = [](F f,Args... args){
try{
f(args...);
}catch(exception& e){
std::cout << "Caught exception" <<std::endl;
}
};
auto bound = std::bind(wrap, std::forward<F> (f),
std::forward<Args>(args)...);
bound();
}
int main()
{
gen(foo,5);
return 0;
}
I cannot figure out how to pass a function pointer to either a lambda expression or a bind call. There seems to be an error message when calling bound (). Can someone give me advice or say if there is something that I misunderstand?
source
share