C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Weird template behavior, is it design or bug?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Hector
Guest





PostPosted: Sun Jan 18, 2004 1:21 am    Post subject: Weird template behavior, is it design or bug? Reply with quote



Hi all,

I don't know whether the following behavior is by design or an error
of my compiler. The problem is with templates not being instantiated
when needed if the definition of the template is in a different file
than the declaration. Consider the following three files

/****** A.h *******/
class foo {
public:
template <class R> void test(R &);
};

/****** A.cpp ******/
#include "A.h"

template <class R> void foo::test(R &) {}

void template_works () {
foo f;
int x;

f.test(x);
}

/******* B.cpp ******/
#include "A.h"

void template_doesnt_work () {
foo f;
int x;
char y;

f.test(x); //compiles fine
f.test(y); //error LNK2019: unresolved external symbol
}

Something similar happens with inline functions (at least in my
compiler). Is it broken or is there some rationale for this behavior?

Cheers,

Hector C.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Carl Daniel
Guest





PostPosted: Sun Jan 18, 2004 11:24 am    Post subject: Re: Weird template behavior, is it design or bug? Reply with quote



Hector wrote:
Quote:
Hi all,

I don't know whether the following behavior is by design or an error
of my compiler. The problem is with templates not being instantiated
when needed if the definition of the template is in a different file
than the declaration.

This is normal and expected behavior for any compiler/linker that uses the
"inclusion model" of template instantiation (you're apparently using VC++,
which definitely uses this model).

The C++ standard also provides the "separation model", supported by the
export keyword. To date, only Comeau C++ has implemented support for
export, so it's not really an option for portable code.

The normal solution for "inclusion model" compilers is to put template
definitions into header files and include those files in every translation
unit where the template is referenced. Normally the linker will take care
of merging duplicate instantiations that may occur if a template is
instantiated in multiple transaction units with the same template
parameters.

The problem with the strategy that you illustrated is that template
definitions are not compiled to object code. Rather, a template is a
"recipe" that the compiler can use to generate (object) code. As such,
there's no manifestation of the template definition in the object files
produced by the compiler - only instantiations of the templates appear in
object files.

-cd


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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.