为什么用C ++中的'inline'关键字?

为什么用C ++中的'inline'关键字?

问题描述:

我刚刚研究了 inline 的使用和优点/缺陷en-us / library / z8y1yy88.aspxrel =nofollow> Microsoft网站,我理解所有这一切。

I've just been researching the use and benefits/pitfalls of the C++ keyword inline on the Microsoft Website and I understand all of that.

我的问题是:if编译器评估函数以查看是否内联它们将导致代码更高效,并且 inline 关键字只是编译器的一个SUGGESTION,为什么要关心该关键字?

My question is this: if the compiler evaluates functions to see if inlining them will result in the code being more efficient and the inline keyword is only a SUGGESTION to the compiler, why bother with the keyword at all?

编辑:很多人对我使用 __ inline 而不是 inline 。我想指出, __ inline 是Microsoft特定的一个:所以它没有错,它只是不一定是你习惯。固定网站链接)

A lot of people are moaning about my use of __inline instead of inline. I'd like to point out that __inline is the Microsoft specific one: so it's not wrong, it's just not necessarily what you're used to. (Also fixed the website link)

EDIT2:重新格式化问题以指示 inline 关键字

Re-formatted the question to indicate the inline keyword (used across all of C++) instead of the Microsoft-specific __inline keyword.

首先, ,它不是 __ inline ,而是 inline

Firstly, it is not __inline, but inline.

其次,在单一定义规则中的效果 inline 是不可否认的。它允许你多次定义函数,并让编译器处理它。

Secondly, the effect inline has within the One Definition Rule is undeniably significant. It allows you to define the functions multiple times and have the compiler to handle it.

第三,对于实际inilining这是你的方式来表达你的意见关于该函数不仅对编译器,而且对那些谁可能读取你的代码以后。在许多情况下,这是一种释放蒸汽的方式,所以说。基本上,这是一种方式告诉他人和你自己:我觉得这个功能太小(或太专门)来证明调用开销,所以不要让我负责这个耻辱,我做了我所有如果不是为了你的愚蠢的公司范围的编码标准,我会做一个宏。在这方面,它是一种形式化的注释

Thirdly, with regard to the actual inilining this is your way to express your opinion about that function not only to the compiler but also to those who might read your code later. In many cases it is a way to let off the steam, so to say. Basically, it is a way for you to tell the others and yourself: "I feel that this function is too small (or too specialized) to justify the calling overhead, so don't hold me responsible for this travesty. I did all I could. If not for your stupid company-wide coding standard, I would've made it a macro". In that regard it is a sort of formalized comment.

第四,看到你使用关键字的实现特定拼写,请注意,一些实施提供了替代关键字,让您有机会更有效率地说服您的愿望,让您的功能内联。在MS编译器中, __ forceinline

Fourthly, seeing that you used an implementation-specific spelling of the keyword, I'd note that some implementations offer you alternative keywords that give you the opportunity to be more... er... persuasive in your desire to have that function inlined. In MS compiler that would be __forceinline.