Monday, June 18, 2007

4.5 Inline function

By declaring the access functions inline, we maintain the performance efficiency of direct member access, while achieving the encapsulation of a function call. Inline functions are also a safe alternative to the #define preprocessor macor expansion relied on so heavily in C. Inline keyword is only a request.
Formal argument
What actually happens during an inline expansion?
1. In general, each formal argument is replaced with its corresponding actual argument.
2. If the actual argument exhibits a side effect, temporary objects are introduced.
3. If the actual argument is a constant expression, it is evaluated prior to its substitution.
Local variables
In general, each local variable within the inline function must be introduced into the enclosing block of the call as a uniquely named variable. If the inline function is expanded multiple times within one expression, each expansion is likely to required its own set of the local variables.
inline int min(int i, int j)
{
int minval = i
return minval;
}
{
int local_var;
int minval;
//...
minval = min(val1, val2);
}
will be transformed into:
{
int local_var;
int minval;
//mangled inline local variable
int __min_lv_minval;
minval = (__min_lv_minval = val1
}
Summary: arguments with side effects, multiple calls within a single expression, and multiple local variables within the inline itself can all create temporaries that the compiler may or may not be able to remove.

No comments: