 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Hector Guest
|
Posted: Sun Jan 18, 2004 1:21 am Post subject: Weird template behavior, is it design or bug? |
|
|
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
|
Posted: Sun Jan 18, 2004 11:24 am Post subject: Re: Weird template behavior, is it design or bug? |
|
|
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 |
|
 |
|
|
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
|
|