Is it possible to force a function not to be inlined?

前端 未结 9 603
攒了一身酷
攒了一身酷 2020-12-01 11:30

I want to force a little function not to be compiled as inline function even if it\'s very simple. I think this is useful for debug purpose. Is there any keyword to do this?

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 12:17

    Please remember that inlining is relevant at the function call site, the same function can be inlined in some situations and not inlined in other.

    If your function is visible outside the compilation unit then even if it's inlined in all the current places it's used, the body of the function must still be available for anyone who wants to call it later on (by linking with the object file).

    In order to have a call site not inlined you can use a pointer to a function.

    void (*f_ptr)(int); // pointer to function
    volatile bool useMe = true; // disallow optimizations 
    if (useMe)
       f_ptr = myFunc;
    else
       f_ptr = useOtherFunc;
    
    f_ptr(42); // this will not be inlined
    

提交回复
热议问题