c++ - boost::bind implicit conversion to boost::function or function pointer -


i'm using boost::function this:

template<class t1> void run(boost::function<void (t1)> func, string arg) {     t1 p1 = parse<t1>(arg);     func(p1); } 

when used this, ok:

void test1(int i) {     cout << "test1 i=" << << endl; }  ...  boost::function<void (int)> f = &test1; run(f, "42"); 

i want able pass raw function pointer directly, overload run() function this:

template<class t1> void run(void (*func)(t1), string arg) {     t1 p1 = parse<t1>(arg);     (*func)(p1); }  ...  run(&test1, "42"); // ok 

now, want able pass result of boost::bind run() function. this:

void test2(int i, string s) {     cout << "test2 i=" << << " s=" << s << endl; }  ...  run(boost::bind(&test2, _1, "test"), "42"); // edit: added missing parameter 42 

but wont compile: edited

bind.cpp: in function ‘int main()’: bind.cpp:33:59: error: no matching function call ‘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 overload run() accept boost::bind()?

edit 2

i know can this:

boost::function<void (int)> f = boost::bind(&test2, _1, string("test")); run(f, "42"); 

but i'd usage less verbose.

edit 3

changed run() prototype run(boost::function<void (t1)>, t1) run(boost::function<void (t1)>, string) elaborate actual use case. ref. igor r.'s answer

the entire source file may obtained here

neither function nor result type of bind convertible function pointer, can't pass them run function current signature.

however, can change run signature allow accepting callable:

template<class f, class a1> void run(f f, a1 arg) {   f(arg); } 

now can pass pointer function, binder, boost::function or ever callable wish - long expects 1 argument. (note however, trivial signature run wouldn't forward arguments f seamlessly.)


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -