I am writing a class to log string messages sent to it. To increase performance, I may at some point want to disable logging at compile time. I intend to make all the functions of the log class inline and surround their code with conditional directives as follows.
1 2 3 4 5 6 7 8 9 10 11
class Log {
// etc
public:
// etc
void WriteMessage(std::string msg)
{
#ifndef NOLOG
// all the function code
#endif
}
};
Therefore, by defining the macro NOLOG, I will make the function WriteMessage inline and empty.
EDITED: I know there is no overhead for inline functions due to passing parameters. Does that mean that an empty inline function result in no processor instructions whatsoever in the final executable?
The compiler is what actually makes the decision, a compiler may recognise that the function does nothing and ignore it or actually generate code for it, that may happen whether the function was declared inline or not
I ran some tests in VC++ and whenever the function was empty, inline or not, the file size was exactly the same as when the function wasn't included in the file at all. Thus it looks like I can just get on with it and trust the compiler with optimisation as you said :)
Well when I actually wrote the code in the Visual C++ IDE, I didn't indent them, partially because of what you said, and partially because that's how the IDE arranges it :P
But when I typed it up on here, I thought I'd indent it since I try to make everything as neat as I possibly can before I post ;)