Reputation: 2263
I'm using boost::function like this:
template<class T1>
void run(boost::function<void (T1)> func, string arg)
{
T1 p1 = parse<T1>(arg);
func(p1);
}
When used like this, everything is ok:
void test1(int i)
{
cout << "test1 i=" << i << endl;
}
...
boost::function<void (int)> f = &test1;
run(f, "42");
I want to be able to pass the raw function pointer directly, so I overload the run() function like this:
template<class T1>
void run(void (*func)(T1), string arg)
{
T1 p1 = parse<T1>(arg);
(*func)(p1);
}
...
run(&test1, "42"); // this is OK now
Now, I want to be able to pass the result of boost::bind to the run() function. Like this:
void test2(int i, string s)
{
cout << "test2 i=" << i << " s=" << s << endl;
}
...
run(boost::bind(&test2, _1, "test"), "42"); // Edit: Added missing parameter 42
But this wont compile: Edited
bind.cpp: In function ‘int main()’:
bind.cpp:33:59: error: no matching function for call to ‘run(boost::_bi::bind_t<void, void (*)(int, std::basic_string<char>), boost::_bi::list2<boost::arg<1>, boost::_bi::value<std::basic_string<char> > > >, std::string)’
bind.cpp:33:59: note: candidates are:
bind.cpp:7:6: note: template<class T1> void run(boost::function<void(T1)>, std::string)
bind.cpp:14:6: note: template<class T1> void run(void (*)(T1), std::string)
How should I overload run() to accept boost::bind()?
Edit 2
I know I can do it like this:
boost::function<void (int)> f = boost::bind(&test2, _1, string("test"));
run(f, "42");
But I'd like the usage to be less verbose.
Edit 3
Changed run() prototype from run(boost::function<void (T1)>, T1)
to run(boost::function<void (T1)>, string)
to elaborate the actual use case. Ref. Igor R.'s answer
The entire source file may be obtained here
Upvotes: 0
Views: 1768
Reputation: 15075
Neither function
nor the result type of bind
are convertible to a function pointer, so you can't pass them to run
function with its current signature.
However, you can change run
signature to allow it accepting any callable:
template<class F, class A1>
void run(F f, A1 arg)
{
f(arg);
}
Now you can pass a pointer function, a binder, boost::function
or what ever callable you wish - as long as it expects 1 argument. (Note however, that with this trivial signature run
wouldn't forward the arguments to f
seamlessly.)
Upvotes: 1