内联函数是在调用处直接嵌入函数体源的函数,实现机制和函数完全不同,确实和宏几乎一样,但它的参数类型检查却和函数一样,对于类型不一致还进行类型转换,对比之下,宏就不会进行这样的检查和转换。还有一个很大的区别,内联函数参数传递时值只取一次,而宏由于是直接替换,所以可以计算多遍。比如:
int x=5,y=2;
int k=max(x++,y++);
如果max是内联函数,则k=6;如果是宏,则k=7;
(凡有循环语句或switch语句等的函数不能成为内联函数)
MSDN says:
The __inline keyword is equivalent to inline.
Even with __forceinline, the compiler cannot inline code in all circumstances. The compiler cannot inline a function if:
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 returns an unwindable object by value, when compiled with /GX, /EHs, or /EHa.
The function receives an unwindable copy-constructed object passed by value, when compiled with /GX, /EHs,, or /EHa.
The function is recursive and not accompanied by #pragma inline_recursion(on). With the pragma, recursive functions can be inlined to a default depth of eight calls. To change 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.
If the compiler cannot inline a function declared with __forceinline, it generates a level 1 warning (4714).
Recursive functions can be substituted inline to a depth specified by the inline_depth pragma. After that depth, recursive function calls are treated as calls to an instance of the function. The inline_recursion pragma controls the inline expansion of a function currently under expansion. See the Inline-Function Expansion (/Ob) compiler option for related information.
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
#include <afx.h>
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h> // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
#include <iostream>
#include "a.h"
#include "b.h"
extern a obja;
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
// stdafx.cpp : source file that includes just the standard includes
// test.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
a obja;
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
// test.cpp : Defines the entry point for the console application.
//