Why Bother With the 'inline' Keyword in C++?

元气小坏坏 提交于 2019-12-02 01:18:29

Firstly, it is not __inline, but 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.

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.

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.

The keyword is inline and not __inline.

inline is not a mere suggestion it is the best standard compliant way to include a function definition in a header file without breaking the one definition rule.

Judging from the documentation in MSDN, I think it's a purely pragmatic thing, mostly for the benefit of C programs.

Since inline is a valid identifier name in C, another keyword is needed in C.

Personally, I would only consider using it if my source code could end up being shared between C and C++ programs (in Visual Studio, obviously).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!