473,473 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

templates & obj

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.
Jul 19 '05 #1
7 2364
"Steven C." <no****@xxx.com> wrote...
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

Jul 19 '05 #2
"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, assuming of course the the h file is included in multiple .cpp and
all the cpp require the same types.
Jul 19 '05 #3

"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
Jul 19 '05 #4

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


Jul 19 '05 #5
"Steven C." <no****@xxx.com> wrote in message
news:UD******************@twister.socal.rr.com...

"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


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
Jul 19 '05 #6
Victor Bazarov wrote:

"Steven C." <no****@xxx.com> wrote...
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.


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

--
Andre, a simple peasant, had only one thing on his mind as he crept
along the East wall: 'Andre, creep... Andre, creep... Andre, creep.'
-- unknown
Jul 19 '05 #7
"David Rubin" <bo***********@nomail.com> wrote...
[..]
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
Jul 19 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: SK | last post by:
Hello group, I happened to check the review of C++ Templates & Tools (By Scott Robert Ladd) at http://accu.org. The book is in the "Not recommended" category. I will like to hear if you anyone...
2
by: Frank Schmitt | last post by:
Hi! Suppose I have a class template Foo with a member function template bar - so far so good: template <typename T> struct Foo { template <typename U> void bar(const U& u); };
1
by: Matt Garman | last post by:
Is there a generally accepted means of generating reports in C and/or C++? In particular, I have an application with many parameterized text strings that get displayed to a user. These could be...
20
by: verec | last post by:
One problem I've come accross in designing a specific version of auto_ptr is that I have to disntiguish between "polymorphic" arguments and "plain" ones, because the template has to, internally,...
16
by: chameleon | last post by:
I have 2 classes with exactly the same members (all static except dtor/ctor). Classes have different implementantion in only one static member function and first class has one more member...
0
by: Steven Prasil | last post by:
When I start my VisualStudio 2005 it creates automatically new folders: D:\work\Visual Studio 2005 D:\work\Visual Studio 2005\Projects D:\work\Visual Studio 2005\Templates Yes, in VS menu ...
3
by: Szabolcs | last post by:
Consider the attached example. When I try to compile it (with g++ 4.1.2), I get the error message error: no matching function for call to 'fun(A<int>&)' However, if I do not use templates, it...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.