Created
April 16, 2013 07:50
-
-
Save anorm/5394177 to your computer and use it in GitHub Desktop.
Illustrates the problem described here: http://stackoverflow.com/questions/15895365/boostbind-implicit-conversion-to-boostfunction-or-function-pointer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/bind.hpp> | |
#include <boost/function.hpp> | |
#include <sstream> | |
using namespace std; | |
template<class T1> | |
T1 parse(string arg) | |
{ | |
istringstream iss(arg); | |
T1 ret; | |
iss >> ret; | |
return ret; | |
} | |
template<class T1> | |
void run(boost::function<void (T1)> func, string arg) | |
{ | |
T1 p1 = parse<T1>(arg); | |
func(p1); | |
} | |
template<class T1> | |
void run(void (*func)(T1), string arg) | |
{ | |
T1 p1 = parse<T1>(arg); | |
(*func)(p1); | |
} | |
void test1(int i) | |
{ | |
cout << "test1 i=" << i << endl; | |
} | |
void test2(int i, string s) | |
{ | |
cout << "test2 i=" << i << " s=" << s << endl; | |
} | |
int main() | |
{ | |
// First usecase | |
boost::function<void (int)> f = &test1; | |
run(f, "1"); | |
// Second usecase | |
run(&test1, "2"); | |
// Third usecase | |
run(boost::bind(&test2, _1, "Third usecase"), "3"); // <-- this wont compile | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment