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

前端 未结 5 601
忘了有多久
忘了有多久 2021-01-25 08:17

C++ ISO standard says, that:

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

Are there any compilers that IGNORE th

相关标签:
5条回答
  • 2021-01-25 08:41

    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.

    0 讨论(0)
  • 2021-01-25 08:48

    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.

    0 讨论(0)
  • 2021-01-25 08:53

    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.

    0 讨论(0)
  • 2021-01-25 08:54

    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.

    0 讨论(0)
  • 2021-01-25 09:01

    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

    0 讨论(0)
提交回复
热议问题