Re: Can static member variables be declared "inline"?
James Kanze wrote:
Some compilers do instantiate many times, and
count on the linker throwing out all but one, but the better
ones maintain a repository of some sort, and don't reinstantiate
if they don't have to.
That would depend a lot on the compilation strategy used.
Classically each compilation unit (eg. .cc file) is compiled
completely separately and independently into an object file (eg. .o).
That's everything the compiler might be instructed to do: To compile one
..cc file into one .o file, period. The compiler does not know of
anything else. This is the most typical situation when compiling with
makefiles and the 'make' utility.
In this case the compiler has absolutely no way of knowing where else
some templated static variable might be used, so it simply has no other
option than to instantiate it in that object file. It knows of no
others. It can't even know what the object file will be used for. Maybe
it will not be used to link an executable directly, but rather create a
precompiled library (.a or .lib in many system) or a
dynamically-linkable one (.so, .dll). In theory the object file could
even be used to link it to a program written in a completely different
language (although only in theory, as in practice this happens quite
rarely). So in short, it has no other option than to make the
instantiation to the object file.
Is this an "inferior" compiler in your opinion? Because you talk about
other compilers which use a different compilation strategy as "better".