23803

What exactly happens to empty inline functions?

<h3>Question</h3>

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.


<h3>Answer1:</h3>

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.


<h3>Answer2:</h3>

You might or might not be left with a trivially empty function (if for example, the function's address is used to make a pointer, then the function needs to exist).

But all inlined call sites will turn into nothing. (And the compiler should choose to always inline direct calls to a function it can see is empty -- Adam's answer is correct about calls into other translation units making this difficult, but Whole Program Optimization can help even there)

Do note, however, that parameters to an inline function will still be evaluated. They might also get inlined and mostly eliminated, but side effects in parameters will occur. This is rather different from using a #define macro to eliminate the entire log(...) string from the source code. The macro gets rid of the parameter computations, too.

来源:https://stackoverflow.com/questions/21657455/what-exactly-happens-to-empty-inline-functions

Recommend

  • #ifdef DEBUG with CMake independent from platform
  • Where is X-FRAME-OPTIONS: DENY coming from on Django site via nginx?
  • Error in float to long conversion [duplicate]
  • python delayed execution of print command?
  • Whitescreen of death after pulling from git repo (ReactJS, Nginx)
  • LDAP authentication with django REST
  • CSS hack for Chrome on Mac Only - No JavaScript -
  • TKinter - How to stop a loop with a stop button?
  • How to make a Model attribute global?
  • Android best way to display html text
  • Nested information in Firebase
  • Iteratively insert rows into a postgreSQL database using SQLAlchemy in python
  • Can testing with real adMob ads after the app's development results in adMob's policy viol
  • Deducing the return type of a standalone function
  • How well does Entity Framework 6 support .NET 4.0?
  • removeAllCachedResponses can not clear sharedURLCache?
  • How to use a string like a variable in Visual Basic
  • Executing JavaScript function in Code behind — How to Get the Return Value in Code Behind
  • Possible steps to improve SQL Server query performance
  • Memory leaks when using pthead_exit() to exit thread
  • How to bend a cylinder in three.js?
  • Unknown type name with typedef struct in C
  • how to change the black color to Red with opencv python
  • Extract table from Powerpoint
  • MayAVI install on Python 3.6 [duplicate]
  • Unity Resources.load() won't work with external dll
  • Shiny - change the size (padding?) of dropdown menu (select tags) smaller
  • How to resolve this in PHPUnit where it is asking me to set KERNEL_DIR in my phpunit.xml?
  • Tensorflow Dataset API restore Iterator after completing one epoch
  • Why isn't stemDocument stemming?
  • How to handle div that is created dynamically in a table
  • Bind selectedDates Aggregation for Calendar
  • Call Microservice from another Microservice within Docker