U
Uncle Steve
Not quite - it only has to be in the same translation unit. A
translation unit consists of a given source file plus all of the other
files merged into it by #include statements. The use of the function
must also occur within the scope of the inline declaration, which
basically means that the declaration should occur prior to the use.
These requirements also apply to macros, though the scope rules are a
bit different for them.
Sorry, I'm not up on the technical nomenclature pertinent to
compilers. I *meant* to say 'translation unit', but all my brain had
was 'source file'.
If you replace your function-like macro, wherever it is that you have it
defined, with an inline function definition in that same location, that
function will be usable pretty much wherever the the macro was usable.
It's probably feasible to come up with pathological contexts where a
simple in-place replacement won't work, but such contexts are not the norm.
Agreed.
Perhaps not, but it's one that most popular modern compilers do
routinely and very well; that's part of what makes them popular.
Inline functions are easy to use, but who can measure the performance
advantage of inlining versus the code size tradeoff? I have no idea
how to accurately measure and quantify the real advantages, which may
be different for every architecture and application. I suspect the
trend of blindly setting CFLAGS=-O9, which is not all that uncommon in
makefiles, masks a profound ignorance of what really goes on in a
running program. I also suspect that the complex interaction of
compile and run-time factors defies simplistic optimization
heuristics.
Any decent compiler can be relied upon to take those issues into
consideration when deciding whether or not to inline a function. This is
something it can decide, independently of whether or not the function is
declared inline. As far as actual inlining is concerned, the 'inline'
keyword is only a hint, which a compiler is free to ignore; and it's
perfectly legal for a compiler to decide to inline a function that is
not declared 'inline', as long as doing so doesn't change the observable
behavior. In fact, that was one of the arguments given against the
introducing of the 'inline' keyword. Any static function written to meet
the same special requirements that currently apply to 'inline' functions
could already have been inlined, even if not so declared, so long as the
compiler thought that doing so would be a good idea.
At least these compilers allow one to individually tune the
optimization characteristic of the compilation process. If you don't
want inlining, it's trivial to turn it off.
Unless you know a lot more about the target platform than your compiler
does, it's probably best to rely upon it to make inlining decisions.
All things being equal, that is decent advice.
Defining a function (whether or not you use 'inline') gives it that
option. Defining it as a function-like macro does not - it only allows
inlining. Well, technically, I suppose a sufficiently sophisticated
compiler could perform anti-inlining: recognizing a common code pattern,
and replacing it wherever used with a call to a compiler-generated
function definition. The fact that the common code was the result of a
macro expansion would make it easier to recognize the feasibility of
such an optimization. However, and it seems to me to be a harder
optimization to perform than inlining, and I doubt that it is a common
feature even of the most sophisticated compilers.
That kind of optimization would be the comp-sci equivalent of gilding
the lily.
Regards,
Uncle Steve