E
Elpoca
Hi:
What rules govern the inlining of templated functions and templated
class methods?
It has always been my understanding that both templated functions and
templated class methods were always expanded inline. Recently, I
replaced an explicitly written function with one implemented using
templates (and partial-template specialisation), in the belief that the
the latter would be entirely inlined-away by the compiler, leaving the
same machine code as the explicitly written function and no
function-call overhead. However, analysis of the machine code produced
by my compiler (Visual C++ 7.1) indicates that the templated functions
are not entirely being removed. In other words, there are still
function calls. By forcing inline expansion (using the non-standards
conformant __forceinline keyword), I can remove the function calls and
make my templated implementation produce exactly the same machine code
as the explicitly written function (and, more importantly, the same
performance). MingW is also not inlining the templated code.
So, my questions are:
1. Why aren't my templated functions and templated class methods calls
being removed (i.e., optimised away) by inlining?
2. Are templated functions and templated class methods supposed to be
inlined by the compiler?
3. Does the inline keyword have any impact when used with templated
functions and templated class methods?
4. Is there something fundamentally wrong with my understanding and
analysis of all of this?
Long e-mail, but I would appreciate any clarification!
Thanks,
El
What rules govern the inlining of templated functions and templated
class methods?
It has always been my understanding that both templated functions and
templated class methods were always expanded inline. Recently, I
replaced an explicitly written function with one implemented using
templates (and partial-template specialisation), in the belief that the
the latter would be entirely inlined-away by the compiler, leaving the
same machine code as the explicitly written function and no
function-call overhead. However, analysis of the machine code produced
by my compiler (Visual C++ 7.1) indicates that the templated functions
are not entirely being removed. In other words, there are still
function calls. By forcing inline expansion (using the non-standards
conformant __forceinline keyword), I can remove the function calls and
make my templated implementation produce exactly the same machine code
as the explicitly written function (and, more importantly, the same
performance). MingW is also not inlining the templated code.
So, my questions are:
1. Why aren't my templated functions and templated class methods calls
being removed (i.e., optimised away) by inlining?
2. Are templated functions and templated class methods supposed to be
inlined by the compiler?
3. Does the inline keyword have any impact when used with templated
functions and templated class methods?
4. Is there something fundamentally wrong with my understanding and
analysis of all of this?
Long e-mail, but I would appreciate any clarification!
Thanks,
El