Skip to content

Instantly share code, notes, and snippets.

@albertz
Created December 25, 2011 18:43
Show Gist options
  • Save albertz/1519596 to your computer and use it in GitHub Desktop.
Save albertz/1519596 to your computer and use it in GitHub Desktop.
functions vs functors
// g++ functor_demo.cpp -c -S
// With this set to `inline`, G++ will inline both examples.
// With this empty, G++ will inline none of them.
#define OPT_INLINE inline
OPT_INLINE int f1(int) { return 42; }
template< int (*f)(int) >
void do_sth1() { f(0); }
void test1() {
do_sth1<f1>();
}
// ----- vs -----
struct F2 {
OPT_INLINE int operator()(int) { return 42; }
};
template< typename T >
void do_sth2(T f) { f(0); }
void test2() {
do_sth2(F2());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment