 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Christof Warlich Guest
|
Posted: Sun Aug 13, 2006 9:10 am Post subject: template implementation not seen by the compiler |
|
|
Hi,
consider this:
template<typename T> class X {
public:
void doSomething(T t);
};
int main(void) {
X<int> x;
x.doSomething(4);
return 0;
}
It compiles fine but the linker is missing the implementation of
doSomething().
Why may it ever make sense that the compilation of this works fine? The
compiler already _knows_ that it could not instantiate doSomething() for
type int since it has not seen the (template) implementation of
doSomething(). Thus, linking will _always_ fail. Or do I miss a way to
pass the doSomething() implementation for a specific type to the linker
from another object file?
Thanks for any help,
Christof |
|
| Back to top |
|
 |
Rolf Magnus Guest
|
Posted: Sun Aug 13, 2006 9:10 am Post subject: Re: template implementation not seen by the compiler |
|
|
Christof Warlich wrote:
| Quote: | Hi,
consider this:
template<typename T> class X {
public:
void doSomething(T t);
};
int main(void) {
X<int> x;
x.doSomething(4);
return 0;
}
It compiles fine but the linker is missing the implementation of
doSomething().
Why may it ever make sense that the compilation of this works fine? The
compiler already _knows_ that it could not instantiate doSomething() for
type int since it has not seen the (template) implementation of
doSomething().
|
It might not need to.
| Quote: | Thus, linking will _always_ fail.
|
No. There could be another translation unit that defines doSomething() and
also instantiates the template for int. Then there would be an
implementation, but only the linker could find it. |
|
| 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
|
|