Are there any compilers that IGNORE C++ standard about default inline functions?

跟風遠走 提交于 2019-12-02 12:00:06

问题


C++ ISO standard says, that:

"A function defined within a class definition is an inline function."

Are there any compilers that IGNORE this rule?
(please, do not mistake inline with inlineD - my question is if there is a compiler, that wont put there that inline suggestion that it should)


回答1:


You seem to be misunderstanding what "inline" means. It doesn't mean functions will automatically be inlined; according to 7.1.2-2 it indicates that inline substitution is to be preferred.

Therefore, you can't tell whether a function is labeled inline or not from the code, since the compiler is free to decide one way or another. It's just a compiler hint.




回答2:


The standard says that all compilers can ignore inline requests, whether implicit or explicit. Whether or not they do so will nornally depend on whether the function can practically be inlined - for example recursive functions cannot be.

Edit: Just to clarify - the questioner is ignoring this, from the previous para in the standard to that he quoted from:

An implementation is not required to perform this inline substitution at the point of call




回答3:


I suspect your test is flawed. You can't test with only one such file whether the compiler ignores the inline specifier or not.

You need to include the header containing the inline function definition and include it into multiple implementation files that are then linked together. If you get linker errors about multiple defined instances of that functions, then the compiler is ignoring the inline specifier regarding its most important property: Allowing it to be defined multiple times across the entire program while still retaining the same address for it and its local static variables.

What your test probably checks is whether or not the compiler inlines the call to the function, which is actually only a hint to the compiler and only a small of many other more important consequences of the inline specifier. If the compiler does not not inline a call to the function, it is fine doing so. The standard does not require it to do anything in this matter.




回答4:


See my answer to a very similar question: When is "inline" ineffective? (in C)

Summary: inline is only required to allow multiple definitions. Any function calling changes is purely optional.




回答5:


Compiler's usually inline based on the number of calls to the function, the number of pseudo-instructions in the function, and a bunch of other things. Take a look at the GCC documentation on optimization options for an idea of how it does things. Basically, the inline keyword is just a hint that bumps up the likelihood that the compiler will inline. The actual decision to inline is usually complex.



来源:https://stackoverflow.com/questions/654452/are-there-any-compilers-that-ignore-c-standard-about-default-inline-functions

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