First off, function aliasing is a major part of the whole 'object-oriented programming' concept. Don't write code that fights its own structure. |
Well, like it or don't, but eliminating it is the only impact of inlined functions you will get. Note also that inlining as available in C++ doesn't eliminate it on the user level, it does so on the compiler/optimizer level. And that's a Good Thing for sure.
Secondly, you can't defeat the One Definition Rule. Just putting a function in a header doesn't mean that it gets inlined. |
That's one of the "right in most cases" things. It's right that the word 'inline' has little or (for most compilers) no effect on the decision of the compiler. But to quote from the ODR:
Every program shall contain exactly one definition of every non-inline function or object that is used in that program; |
... where 'non-inline function' doesn't refer to what the compiler does. It refers to what the language syntax and semantic is all about. So while the compiler pretty much can do what it wants (e.g. copy it to another cpp file and make it a 'regular' function), on the language level the ODR is 'special' for inline functions.
Finally, eliminating function calls makes a significant difference when speed is an issue. |
Maybe. If the code doesn't need to be copied into the cache and some other assumptions hold true. But your compiler is better with optimizations than you are, trust me that (or, if you don't, spend some time with the nice folks from the gcc project or any other major compiler vendor).
Furthermore, most people I know who optimize on the 'inline' level have little or no idea about the real speed issues of their code, which is rather in the magnitude of using bubble sort instead of something faster.
Remember, though, that virtual functions (AKA aliased functions) cannot be inlined. |
You make that sound as if "virtual function" = "aliased function" = function that has a 'problem' with aliasing. That's not what I meant by aliasing (and what the word aliasing is used in this context for generally). It's the lack of possibilities a compiler has to optimize beyond function bounderies. And that's an issue for any function call (except inlined functions). So while it's right that you can't inline virtual functions (how should you?!?), aliasing might be a problem, be it one that's solved by your compiler better than by your suggestions.
So, all in all, my advice is: don't use inline (or register, or auto, or goto). Unless you want to save yourself a .cpp file for a one-line-function whose definition is unlikely to change like
1 2 3 4
|
std::ostream& operator<<(std::ostream& out, const C& c)
{
return c.print(out);
}
|