What is wrong with using inline functions?

后端 未结 12 1206
滥情空心
滥情空心 2020-11-29 01:51

While it would be very convenient to use inline functions at some situations,

Are there any drawbacks with inline functions?

Conclusion:

相关标签:
12条回答
  • 2020-11-29 02:12

    It worth pointing out that the inline keyword is actually just a hint to the compiler. The compiler may ignore the inline and simply generate code for the function someplace.

    The main drawback to inline functions is that it can increase the size of your executable (depending on the number of instantiations). This can be a problem on some platforms (eg. embedded systems), especially if the function itself is recursive.

    I'd also recommend making inline'd functions very small - The speed benefits of inline functions tend to diminish as the function grows in size. At some point the overhead of the function call becomes small compared to the execution of the function body, and the benefit is lost.

    0 讨论(0)
  • 2020-11-29 02:13
    1. As other people said that inline function can create a problem if the the code is large.As each instruction is stored in a specific memory location ,so overloading of inline function make a code to take more time to get exicuted.

    2. there are few other situations where inline may not work

      1. does not work in case of recursive function.
      2. It may also not work with static variable.
      3. it also not work in case there is use of a loop,switch etc.or we can say that with multiple statements.
      4. And the function main cannot work as inline function.
    0 讨论(0)
  • 2020-11-29 02:14

    Inlining larger functions can make the program larger, resulting in more instruction cache misses and making it slower.

    Deciding when a function is small enough that inlining will increase performance is quite tricky. Google's C++ Style Guide recommends only inlining functions of 10 lines or less.

    (Simplified) Example:

    Imagine a simple program that just calls function "X" 5 times.

    If X is small and all calls are inlined: Potentially all instructions will be prefetched into the instruction cache with a single main memory access - great!

    If X is large, let's say approaching the capacity of the instruction cache:
    Inlining X will potentially result in fetching instructions from memory once for each inline instance of X.
    If X isn't inlined, instructions may be fetched from memory on the first call to X, but could potentially remain in the cache for subsequent calls.

    0 讨论(0)
  • 2020-11-29 02:14

    Excessive inlining of functions can increase size of compiled executable which can have negative impact on cache performance, but nowadays compiler decide about function inlining on their own (depending on many criterias) and ignore inline keyword.

    0 讨论(0)
  • 2020-11-29 02:16

    I agree with the other posts:

    • inline may be superfluous because the compiler will do it
    • inline may bloat your code

    A third point is it may force you to expose implementation details in your headers, .e.g.,

    class OtherObject;
    
    class Object {
    public:
        void someFunc(OtherObject& otherObj) {
            otherObj.doIt(); // Yikes requires OtherObj declaration!
        }
    };
    

    Without the inline a forward declaration of OtherObject was all you needed. With the inline your header needs the definition for OtherObject.

    0 讨论(0)
  • 2020-11-29 02:17

    You should also note that the inline keyword is only a request. The compiler may choose not to inline it, likewise the compiler may choose to make a function inline that you did not define as inline if it thinks the speed/size tradeoff is worth it.

    This decision is generaly made based on a number of things, such as the setting between optimise for speed(avoids the function call) and optimise for size (inlining can cause code bloat, so isn't great for large repeatedly used functions).

    with the VC++ compiler you can overide this decision by using __forceinline

    SO in general: Use inline if you really want to have a function in a header, but elsewhere theres little point because if your going to gain anything from it, a good compiler will be making it inline for you anyway.

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