Skip to content

Instantly share code, notes, and snippets.

@stepancheg
Created April 20, 2012 13:11
Show Gist options
  • Save stepancheg/2428389 to your computer and use it in GitHub Desktop.
Save stepancheg/2428389 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <tr1/functional>
using namespace std;
using namespace std::tr1;
using namespace std::tr1::placeholders;
void foo(int x) {
cout << "foo " << x << endl;
}
class qux {
public:
void bar(int x) {
cout << "bar " << x << endl;
}
};
int main(void) {
qux quux;
// low level function pointer
void (*foo_pointer)(int) = &foo;
// low level member function pointer
void (qux::*bar_pointer)(int) = &qux::bar;
(*foo_pointer)(1);
(quux.*bar_pointer)(2);
((&quux)->*bar_pointer)(3);
// high level functions
function<void(int)> foo_function = &foo;
// member function pointer is converted to "plain" function
function<void(qux*, int)> bar_function = &qux::bar;
foo_function(4);
bar_function(&quux, 5);
// what is cool about std::function
function<void(int)> bar_bound = bind(bar_function, &quux, _1);
bar_bound(6);
return 0;
}
// vim: set ts=4 sw=4 et:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment