Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly happens to empty inline functions?

Tags:

c++

inline

I'm writing code (using GCC 4.7.2), where I'm excessively logging stuff during the testing phase in countless positions of the code. These loggings should disappear on the release binary.

I'm doing the logging via a function just like void log(std::string msg);. As these function calls are many and distributed via the whole code in many files, I had the idea to make it an inline function and just give it an empty body for the release binary.

No my question is: what does the compiler do with it? Does the binary just contain the rest of the code without the function, does it contain a nop, or anything else? Could I eliminate the logging code from the binary completely by emptying the inline logging function?

I'm not only interested in an answer to solve the problem but I'm also curious about the compiler's behaviour itself.

like image 292
Marste Avatar asked Dec 09 '25 22:12

Marste


1 Answers

If you want different code between debug and release, then that's an ideal use case for the preprocessor:

#ifdef NDEBUG
#define log(ignored)
#endif

Then you're not leaving anything up to the compiler. You're guaranteed that only the debug version will have the extra calls. This is how assert works, too.

Note that this will also drop the parameter computation. For example, if you have log(get_msg()) then the macro method will drop the call to get_msg() as well. This is probably desirable, but you need to be aware of it.

As for inline, that's entirely up to the compiler. The inline keyword itself is only a hint, it does not obligate the compiler to do anything. The compiler performs its own optimization calculations on whether or not to inline a particular function (that includes inlining functions not marked inline). That typically means a sufficiently high optimization level (i.e. -O3), and that the body of the inline function is visible in that particular compilation unit. For example, if the compiler only sees a declaration but the (maybe empty) function body is in a different .cpp file, then it cannot inline. But yes, if the compiler determines that there are no side effects, it is free to make the whole function disappear.

But again, there's no reason to depend on that when the preprocessor offers such a clean and widely used solution.

like image 185
Adam Avatar answered Dec 11 '25 12:12

Adam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!