Possible Duplicate:
Inline functions in C++
Modern compilers are better than programmers at deciding what should be inlined and what should not. Just like, register
, shouldn't inlining functions be a job for the compiler only, and be considered premature optimization ?
inline
has a double meaning that some are unaware of - it allows a function to be defined in more than one translation unit (i.e. if you define an unbound function in a header and include it from within various translation units, you are forced to declare it inline or the linker would complain about doubly defined symbols).
The second meaning is a hint to the compiler that this function may profit from inlining its machine code at the caller site. You're right, modern compilers/optimizers should be able to figure this out on his own.
My advice is to use inline
only when it is needed (first case) and never to pass an optimization hint to the compiler. This way, this crazy double meaning is resolved in your source code.
inline
is only tangentially related to optimization.
You should choose to apply inline
to a function if you need the exceptions to the one definition rule that it gives you, and leave it out if you don't. Most of the time you can rely on the compiler to perform the appropriate optimizations independent of whether a function is declared inline
or not.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With