$ cat 0.cpp template < typename R, typename T, R ( T::* method )() > static inline R dispatch( T& object ) { return ( object.*method )(); } struct X { virtual ~X(); virtual void f(); }; void test1( X& obj ) { void ( *f )( X& ) = dispatch< void, X, &X::f >; f( obj ); } void test2( X& obj ) { obj.f(); }
g++-4.2.2-RC1 produces unoptimal code for test1: test1(X&): jmp void dispatch<void, X, &(X::f())>(X&) test2(X&): movq (%rdi), %rax movq 16(%rax), %r11 jmp *%r11 void dispatch<void, X, &(X::f())>(X&): movq (%rdi), %rax movq 16(%rax), %r11 jmp *%r11 -- Summary: [missed optimization] trivial static function not inlined. Product: gcc Version: 4.2.2 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: pluto at agmk dot net GCC target triplet: x86_64-gnu-linux http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33546