"Mike Wahler" <mk******@mkwahler.net>
"Steven C." <no****@xxx.com> wrote in message
news:pi*****************@twister.socal.rr.com...
"Victor Bazarov" <v.********@attAbi.com> wrote in When you compile a cpp program that includes a template class in a .h
file where does the compiled code go for that templated class?
Somewhere in the same obj file.
If it went in the
obj for the cpp program seems to me you could end up with multiple
templates of the same type if the .h file was included in multiple .cpp.
Yes, and the linker is supposed to eliminate the multiplicity.
Seems more
straight forward that it would be put in a obj for the .h and the
compiler would just add new ones to the obj for template types that had not been
created by other cpp.
While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.
Or if it was included in every cpp obj then I guess it would have a flag
in the obj for to ignore duplicates of the same template type.
It's all in the function signatures, I guess.
__________________________________________________ _________
Seem sorta stupid that the compiler is compiling the same code over and
over again,
It will compile what you tell it to, as
many times as you tell it.
assuming of course the the h file is included in multiple .cpp and
all the cpp require the same types.
Read again what Victor wrote:
"While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created."
The contents of any #included headers become part of the
text of the file which #includes them. This combination
is known as a 'translation unit', which the compiler
translates into an 'object file'. The compiler cannot
know what the contents of other files are, they're
not input to the compile.
-Mike
__________________________________________________ _________
Mike,
Either you are being argumentative or misunderstood my response to Victor.
I'm sure with a little imagination and thought you could see how compilers
could be smarter about how they managed and compiled templates.
Have a good day. :-)