Does the compiler decide when to inline my functions (in C++)?

后端 未结 9 1500
故里飘歌
故里飘歌 2020-12-06 05:55

I understand you can use the inline keyword or just put a method in a class declaration ala short ctor or a getter method, but does the compiler make the final decision on w

相关标签:
9条回答
  • 2020-12-06 06:50

    Whether or not a fiunction is inlined is, at the end of the day, entirely up to the compiler. Typically, the more complex a function is in terms of flow, the less likely the compiler is to inline it. and some functions, such as recursive ones, simply cannot be inlined.

    The major reason for not inlining a function is that it would greatly increase the overall size of the code, preventing iot from being held in the processor's cache. This would in fact be a pessimisation, rather than an optimisation.

    As to letting the programmer decide to shoot himself in the foot, or elsewhere, you can inline the function yourself - write the code that would have gone in the function at what would have been the function's call site.

    0 讨论(0)
  • 2020-12-06 06:50

    Just to add my 5 cents ...

    I found this Guru of Week article about inlining very useful.

    As far as I remember I read somewhere that even a linker might do inlining, when it links the object files and finds that the code being linked can be inlined.

    0 讨论(0)
  • 2020-12-06 06:52

    As per my knowledge, compiler will automatically make a function you declared inline (or wrote inside a class declaration) non -inline if it finds a loop like for, while etc. This is one example where compiler has the last say in inline functions.

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