Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > why are foos li...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 4 Topic 9755 of 10094
Post > Topic >>

why are foos like vector<T>::size no duplicate symbols for linker?

by vl106 <vl106@[EMAIL PROTECTED] > Jun 28, 2008 at 01:28 AM

{ Please confine responses to standard C++ and generic concepts of
compilation 
and linking, i.e. not going overboard in environment-specific discussion.
-mod }

I have a question concerning the code the compiler
generates for template functions.

- why does linker not complain about ctor being there 2-times?
- does linker "strip away" compiler generated template functions (if
necessary)?
- in MSVC map file: does "i" mean: internally created by compiler?

  0002:00000990       ?f_a@[EMAIL PROTECTED]
                00411990 f   a.obj
  0002:000009c0       ??0?$ARRAY@[EMAIL PROTECTED]
      004119c0 f i a.obj
  0002:00000a10       ?f_b@[EMAIL PROTECTED]
                00411a10 f   b.obj

// a.cpp
#include "h.hpp"
void f_a() {
      ARRAY<int> ia(3);
      //...
}

// b.cpp
#include "h.hpp"
void f_b() {
      ARRAY<int> ia(5);
      //...
}

ARRAY is a minimalistic clone of std::vector. In a.cpp
and b.cpp I use the template class - and thus its (only)
function.

Each .obj file has a definition of ARRAY<int>::ARRAY<int>
as can be seen from assembly files (see below).

Why does linker not complain about duplicate symbols as
e.g. when I define "my_foo" both in a.cpp and b.cpp?

b.obj : error LNK2005: "void __cdecl my_foo(void)" (?my_foo@[EMAIL PROTECTED]
)
already defined in a.obj

In the map file I recognize only

  0002:000009c0       ??0?$ARRAY@[EMAIL PROTECTED]
      004119c0 f i a.obj

that is version from b.obj is stripped away?

; in a.asm

??0?$ARRAY@[EMAIL PROTECTED]
 PROC NEAR	; ARRAY<int>::ARRAY<int>, COMDAT
; _this$ = ecx

; 15   : ARRAY<T>::ARRAY(int size) {

	push	ebp
	mov	ebp, esp
	; ...
	mov	esp, ebp
	pop	ebp
	ret	4
??0?$ARRAY@[EMAIL PROTECTED]
 ENDP ; ARRAY<int>::ARRAY<int>

; in b.asm

??0?$ARRAY@[EMAIL PROTECTED]
 PROC NEAR ; ARRAY<int>::ARRAY<int>, COMDAT

; _this$ = ecx

; 15   : ARRAY<T>::ARRAY(int size) {

	push	ebp
	mov	ebp, esp
	; ...
	mov	esp, ebp
	pop	ebp
	ret	4
??0?$ARRAY@[EMAIL PROTECTED]
 ENDP ; ARRAY<int>::ARRAY<int>


#ifndef H_HPP
#define H_HPP

template<class T>
class ARRAY {
public:
	ARRAY(int size);
	// further member foos
private:
	T* data_;
};

template<class T>
ARRAY<T>::ARRAY(int size) {
	data_ = new T[size];
}
#endif // H_HPP

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 4 Posts in Topic:
why are foos like vector::size no duplicate symbols for linke
vl106 <vl106@[EMAIL PR  2008-06-28 01:28:19 
Re: why are foos like vector::size no duplicate symbols for l
"vl106" <vl1  2008-06-28 16:16:50 
Re: why are foos like vector::size no duplicate symbols for
Pavel Minaev <int19h@[  2008-06-28 16:28:37 
Re: why are foos like vector::size no duplicate symbols for l
Alberto Ganesh Barbati &l  2008-06-28 16:58:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 22:36:59 CDT 2008.