Did anyone notice that @Peter87 is about to flip the odometer (post 9999 as I write this).
(And yes, I realize I am inviting another wall of text.) |
Hey, I resemble that remark.
@doug4,
At this point I see you've heard from a number of viewpoints confirming the overall notion that the "inline" is commonly misunderstood to mean optimization, and that most ignore that it's main purpose to inform the linker that duplicate code is coming, typically from function definitions in header files.
Peter87 points out the new use for variables, which offer a convenience over the notion of declaring data as extern, then carefully choosing some translation unit to define it once and only once.
That's the opposing point with functions, too. Essentially stand alone functions (in particular) are all "considered" to be extern, which is to say the declaration found in a header file may be found by the linker in some other translation unit at link time. In that situation if, by some mistake, a definition appeared in two or more translation units it is a linker error (or at least a warning), since the linker can't decide for you (even though some switching might force it to) which of the duplicates to use.
The inline keyword, when applied to functions, addresses that opposite scenario typically implemented as a definition in a header file where such duplicates are to be expected and because they come from the same text in each repetition, it should be safe to assume just one of them is applicable (and if it is short, emitted as inline assembler, leaving no actual function call).
What many don't stop to think is a situation that comes, often by accident, where a function declared in a header, provided in one translation unit, is short (and appears early in the CPP file). Later in that CPP file, the compiler processes a function that calls this short function. It is not declared inline. It isn't defined in the header. Yet, the optimizer is just as likely to emit this short function as inline machine code because it fits all the requirements (and it now knows that code since the short function was already compiled).
This leads to a great frustration and name calling of various compilers, where programmers think the compiler is refusing to optimize and emit inline machine code for their functions when declared inline. They assume the primary purpose of inline is about emitting inline machine code. I'm not a language lawyer (and happy not to be), but I'm of the assumption (clearly an unsafe practice) that the standards don't mention much about a guarantee of any optimized inline machine code. Even with the ever popular __force_inline (or whatever a particular compiler might call it), there is no real guarantee of the emitted machine code. Some compilers seem more cooperative than others.
Ok, I'm going to pout in another forum over being exposed as a TL;DR poster