Skip to content

Instantly share code, notes, and snippets.

@anorm
Created April 16, 2013 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anorm/5394177 to your computer and use it in GitHub Desktop.
Save anorm/5394177 to your computer and use it in GitHub Desktop.
#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