templates & obj

S

Steven C.

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? 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. 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.

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.

Just curious how it worked.
 
V

Victor Bazarov

Steven C. said:
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.
Just curious how it worked.

You're lucky to have time to be curious about those things.

Victor
 
S

Steven C.

Victor Bazarov said:
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, assuming of course the the h file is included in multiple .cpp and
all the cpp require the same types.
 
M

Mike Wahler

Steven C. said:
Somewhere in the same obj file.


Yes, and the linker is supposed to eliminate the multiplicity.


While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.


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
 
S

Steven C.

"Mike Wahler" <[email protected]>

Steven C. said:
Somewhere in the same obj file.


Yes, and the linker is supposed to eliminate the multiplicity.


While creating one obj file, the compiler knows nothing about other
obj files that have been or will be created.


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. :)
 
M

Mike Wahler

Steven C. said:
"Mike Wahler" <[email protected]>



It will compile what you tell it to, as
many times as you tell it.


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

Not at all.
or misunderstood my response to Victor.

Perhaps. Feel free to clarify.

I'm sure with a little imagination and thought you could see how compilers
could be smarter about how they managed and compiled templates.

What compilers *could* be or do doesn't matter. It's
the language definition that does. And what is it
about template handling that you find not 'smart'
enough?

-Mike
Have a good day. :)

I will, thanks. :)

-Mike
 
D

David Rubin

Victor said:
Somewhere in the same obj file.


Yes, and the linker is supposed to eliminate the multiplicity.

How does this work if you are linking to an object file which is
compiled with a different compiler than yours? I am under the impression
that there is no standard for naming (i.e., mangled names) in object
files.

/david
 
V

Victor Bazarov

David Rubin said:
[..]
How does this work if you are linking to an object file which is
compiled with a different compiler than yours? I am under the impression
that there is no standard for naming (i.e., mangled names) in object
files.

You are correct. And it is not supposed to work. C++ language
standard ensures only source-level compatibility between compilers.

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top