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.