讲讲c++ Session 2 内联(inline)函数
【样例】其实还是很有用的
//函数样例
inline int max(int a, int b) { return (a > b) ? a : b; } //c++ #include <iostream.h> int increment(int i); void main() { int i=0; while(i<3) { i=increment(i); cout<<"i is "<<i<<endl; } } //内联函数定义放在main()函数之后 inline int increment(int i) { i++; return i; }
【样例2,vc++的非标准用法】没人要
参考MSDN(懒得翻译了,看不懂的自己谷歌翻译)
The function or its caller is compiled with /Ob0 (the default option for debug builds).
The function and the caller use different types of exception handling (C++ exception handling in one, structured exception handling in the other).
The function has a variable argument list.
The function uses inline assembly, unless compiled with /Og, /Ox, /O1, or /O2.
The function is recursive and not accompanied by #pragma inline_recursion(on). With the pragma, recursive functions are inlined to a default depth of 16 calls. To reduce the inlining depth, use inline_depth pragma.
The function is virtual and is called virtually. Direct calls to virtual functions can be inlined.
The program takes the address of the function and the call is made via the pointer to the function. Direct calls to functions that have had their address taken can be inlined.
The function is also marked with the naked __declspec modifier.
__forceinline is useful if:
inline or __inline is not respected by the compiler (ignored by compiler cost/benefit analyzer)
code portability is not required
inlining results in a necessary performance boost
Example of portable code:
#ifdef _MSC_VER #define INLINE __forceinline /* use __forceinline (VC++ specific) */ #else #define INLINE inline /* use standard inline */ #endif INLINE void helloworld() { /* inline function body */ }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。