Created
December 25, 2011 18:43
-
-
Save albertz/1519596 to your computer and use it in GitHub Desktop.
functions vs functors
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
// 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