63,594
社区成员




void a(){/*...*/}
void b(){/*...*/}
inline void c()
{a();
b();
}
Inline code will not be generated when using a variable number of arguments to a function call (e.g. printf would not be inlined) or a variable sized data type, e.g.
void foo(int n) {
char str[n];
...
}
Compilers have a limit to the depth they will inline functions, e.g. if inline A calls inline B, which calls inline C and so on. This is often configurable through a pragma or command line switch.
There's an example of a function calling another function and how inlining may save you copying the parameters when you pass them to a function and copying the result it returns and stuff. There's also a disclaimer saying that it's just an example and many different things can happen.
/*
The drawback is that fibon_elem() now requires three function calls to complete its operation, whereas previously it required only one. Is this additional overhead critical? That depends on the context of its use. If its performance should prove unacceptable, one solution is to fold the three functions back into one. In C++, an alternative solution is to declare the functions as inline.
*one solution is to fold the three functions back into one.
*an alternative solution is to declare the functions as inline.
根据以上分析,似乎a();b();都是自动变成inline了。你是正确的。否则就不能把three functions back into one。
*/
bool fibon_elem( int pos, int &elem )
{
const vector<int> *pseq = fibon_seq( pos );
if ( ! pseq )
{ elem = 0; return false; }
elem = (*pseq)[ pos-1 ];
return true;
}
// ok: now fibon_elem() is an inline function
inline bool fibon_elem( int pos, int &elem )
{ /* definition same as above */ }