The use of inline, __inline, __forceinline
登录 | 论坛导航 -> 华新鲜事 -> 求学狮城 | 本帖共有 2 楼,分 1 页, 当前显示第 1 页 : 本帖树形列表 : 刷新 : 返回上一页
<<始页  [1]  末页>>
作者:Yup (等级:3 - 略知一二,发帖:858) 发表:2003-02-21 02:42:51  楼主  关注此帖评分:
c++ beginner 的菜鸟问题请问将program分在三个function的问题: 即: lab.cpp data.h data.cpp 为什么我用"inline"在data.cpp(implementation file)里的某些class member function 的前面,compile时就会出错呢? 但是去掉"inline"后就什么问题都没有了,program运行也很正常. 难道分成3个file就不能用"inline" function? 如果能用,用法还是和写在一个file里一样吗? 注:我所用到的inline function都是很小的class member function: eg: class Datalist { .............. int getMonth() const; .............. } inline int Datelist::getMonth() const { return month; }
The use of inline, __inline, __forceinline
The inline and __inline keywords allow the compiler to insert a copy of the function body into each place the function is called. The insertion occurs only if the compiler's cost/benefit analysis show it to be profitable. The __forceinline keyword overrides the cost/benefit analysis and relies on the judgement of the programmer instead. Exercise caution when using __forceinline. Indiscriminate use of __forceinline can result in larger code with only marginal performance gains or, in some cases, even performance losses (due to increased paging of a larger executable, for example).

You cannot force the compiler to inline a function when conditions other than cost/benefit analysis prevent it. You 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 and is not compiled with /Og, /Ox, /O1, or /O2).


Function returns an unwindable object by value and is not compiled with /GX, /EHs, or /EHa).


The function receives a copy-constructed object passed by value, when compiled with /GX, /EHs,, or /EHa.


The function is recursive and is 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 #pragma(inline_depth, n).
If the compiler cannot inline a function declared __forceinline, it generates a level 1 warning (4714).

The inline keyword is available only in C++. The __inline and __forceinline keywords are available in both C and C++. For compatibility with previous versions, _inline is a synonym for __inline.

Using inline functions can make your program faster because they eliminate the overhead associated with function calls. Functions expanded inline are subject to code optimizations not available to normal functions.

The /Ob compiler optimization option determines whether inline function expansion actually occurs
Put your OWN COOL signature here!
欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
作者:Yup (等级:3 - 略知一二,发帖:858) 发表:2003-02-21 12:24:58  2楼 评分:
i think you also have to put inline in class defclass Datalist { .............. inline int getMonth() const; .............. } but normally if I have simple function and want to make it inline. I prefer write mathod body in class def class Datalist { .............. inline int getMonth() const {return month;} .............. }
Why need do this?
The example in MSDN is just like:

class Datalist {
..............
int getMonth() const;
..............
}


inline int Datalist::getMonth() const
{
...
}

欢迎来到华新中文网,踊跃发帖是支持我们的最好方法!原文 / 传统版 / WAP版所有回复从这里展开收起列表
论坛导航 -> 华新鲜事 -> 求学狮城 | 返回上一页 | 本主题共有 2 篇文章,分 1 页, 当前显示第 1 页 | 回到顶部
<<始页  [1]  末页>>

请登录后回复:帐号   密码