hello,
this is my first post in a newsgroup, i hope i do everything right :)
first of all, i am sure there have been a lot of "are templates slow?"
questions around, but i think what i would like to know is not that
general. furthermore, what i ask here may have hardly any impact on
the generated code size or performance; i am not trying to micro-
optimize anything, i am just curious how templates are compiled by
modern compilers of various vendors. i know that the compilers do not
behave the same, so whenever i speak of "compilers", i welcome any
information that is either special to one compiler or widely adopted
by most compilers.
by the way, i have done some searching prior to posting this, but
unfortunately i could not find satisfying answers. i would have done
some test myself if i knew assembler well enough to extract any
information out of the compiler generated code.
Q1)
i have heard that some compilers do not inline template functions or
classes, but i guess this information is quite outdated and modern
compilers inline them just as normal functions/classes, don't they?
Q2)
assume there is a template (member) function funct that has one (or
more) template arguments. funct does a lot of work, but the places it
actually makes any use of the template parameter are relatively rare.
so, all instatiations of funct have a lot of code in common, the
generic code is rather small. do compilers somehow "merge" that common
code so it is not duplicated everytime a new instantiation of funct
occurs?
Q3)
i am currently reading "Modern C++ Design" by Alexandrescu. There, he
demonstrates various techniques to evaluate expressions (might be the
wrong term in this case) at compile time. as an example, i wondered in
chapter 2.4 (here is the code in question: http://rafb.net/p/C9RwGV80.html
) how far compilers really optimizes this template.
if, for example, i instantiate NiftyContainer as follows:
NiftyContainer<int, falsex;
Here, the compiler could "redirect" every call to x.DoSomething(int*)
directly to x.DoSomething(int*, Int2Type<false>), without calling
x.DoSomething(int*) to avoid the calling overhead. do compilers do
that in practise or is that too complicated or even impossible for
more complex real-world code?
Q4)
as in the question above, Alexandrescu uses overloaded functions with
unused dummy arguments. if the compiler has access to the full source
code of the function, it may note that the argument really is unused
(and thus unnamed) and eliminate the overhead of passing the argument.
however, if the definition of the function is not accessible (for
example, it is "hidden" in a closed-source library), the compiler
cannot know whether the argument is used, so it has to pass it. how do
compilers behave in these situations?
Q5)
Alexandresu says that policy classes are either to be contained within
or inherited by the host classes, which makes sense. since only some
policies (and no traits, if i understood that correctly) have any
member data, the compiler could optimize the need to construct/
destruct the parent policy (in case of inheritance) or the member
instance (in case of containment) away. maybe, but i am not sure
whether this is possible for containment, it even could avoid
allocating the minimum size of one byte for the empty class; when used
with inheritance, this should be no problem, is it?
i hope i have stated my questions in an understandable way :)
thank you for possible answers in advance