I believe this is a known problem, but I couldn't find it anywhere. The inliner at -O2 will attempt to inline any small functions. It will not however try to inline functions will external linkage, which includes all templated functions.
As much C++ code (including libstdc++) contains many small templated functions, this means it is necessary to mark all these functions as inline to ensure they are even considered at -O2. Example: given: void foo() {} void foo2() {foo();} int main() {foo2();} then main() is optimised away to an empty function at -O2. However: template<class T> void foo() {} template<class T> void foo2() {foo<T>();} int main() {foo2<int>();} leaves the calls to foo2 and foo in at -O2. Is there a good reason to not pull inlining of small functions (which in C++ typically consist of simply calling a new function with almost identical parameters) into -O2, even when they have external linkage? (or make an exception for templated functions?) -- Summary: Templated functions no inlined at -O2 Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: minor Priority: P2 Component: middle-end AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: chris at bubblescope dot net CC: gcc-bugs at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20411