For T being an arithmetic type, gcc 4.1 generates what looks like suboptimal assembly code for inline C++ functions that take their argument(s) by const reference (const T&) vs the same functions that take their argument(s) by value (T).
Ideally, the code generated for test_min_ref() and test_min_ptr() below would be the same as for test_min_val(). The code generated for SPARC at the bottom shows that the code for both functions is worse. template <class T> inline const T& min_ref (const T &x, const T &y) { return x < y ? x : y; } template <class T> inline const T* min_ptr (const T *x, const T *y) { return *x < *y ? x : y; } template <class T> inline T min_val (T x, T y) { return x < y ? x : y; } int test_min_ref (int x, int y) { return min_ref (x, y); } int test_min_ptr (int x, int y) { return *min_ptr (&x, &y); } int test_min_val (int x, int y) { return min_val (x, y); } .file "t.cpp" .section ".text" .align 4 .global _Z12test_min_refii .type _Z12test_min_refii, #function .proc 04 _Z12test_min_refii: .LLFB5: cmp %o0, %o1 st %o0, [%sp+68] st %o1, [%sp+72] bge .LL7 add %sp, 68, %o0 jmp %o7+8 ld [%o0], %o0 .LL7: add %sp, 72, %o0 jmp %o7+8 ld [%o0], %o0 .LLFE5: .size _Z12test_min_refii, .-_Z12test_min_refii .global __gxx_personality_v0 .align 4 .global _Z12test_min_ptrii .type _Z12test_min_ptrii, #function .proc 04 _Z12test_min_ptrii: .LLFB6: cmp %o0, %o1 st %o0, [%sp+68] st %o1, [%sp+72] bge .LL13 add %sp, 68, %o0 jmp %o7+8 ld [%o0], %o0 .LL13: add %sp, 72, %o0 jmp %o7+8 ld [%o0], %o0 .LLFE6: .size _Z12test_min_ptrii, .-_Z12test_min_ptrii .align 4 .global _Z12test_min_valii .type _Z12test_min_valii, #function .proc 04 _Z12test_min_valii: .LLFB7: cmp %o0, %o1 bg,a .LL17 mov %o1, %o0 .LL17: jmp %o7+8 nop .LLFE7: .size _Z12test_min_valii, .-_Z12test_min_valii .ident "GCC: (GNU) 4.1.0" -- Summary: suboptimal code for min, max, et al Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: sebor at roguewave dot com GCC build triplet: sparc-sun-solaris GCC host triplet: sparc-sun-solaris GCC target triplet: sparc-sun-solaris http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30738