R
reppisch
Hi Ng,
i tried to instantiate a template class in a different compilation unit
than it is implemented. It seems while linking it together the linker
does not call the compiler to generate the missing functions.
Doing it all in one object seems no problem.
How to reproduce:
--------------
scratchpad.hpp
--------------
template < typename T >
class TemplatedBase
{
public:
T member;
int foo(const T arg);
};
--------------
scratchmod.cpp
--------------
#include "scratchpad.hpp"
template < typename T >
int TemplatedBase<T>::foo (const T arg)
{
return 1;
}
--------------
scratchpad.cpp
--------------
#include "scratchpad.hpp"
int main()
{
char c = 42;
TemplatedBase<char> p;
p.foo(c);
}
--------------
g++ scratchpad.cpp scratchmod.cpp
/cygdrive/c/Temp/ccQbDL3c.o(.text+0x31):scratchpad.cpp: undefined
reference to `TemplatedBase<char>::foo(char)'
Is it a bug or a feature? And how to do it right?
Regards,
Michael
i tried to instantiate a template class in a different compilation unit
than it is implemented. It seems while linking it together the linker
does not call the compiler to generate the missing functions.
Doing it all in one object seems no problem.
How to reproduce:
--------------
scratchpad.hpp
--------------
template < typename T >
class TemplatedBase
{
public:
T member;
int foo(const T arg);
};
--------------
scratchmod.cpp
--------------
#include "scratchpad.hpp"
template < typename T >
int TemplatedBase<T>::foo (const T arg)
{
return 1;
}
--------------
scratchpad.cpp
--------------
#include "scratchpad.hpp"
int main()
{
char c = 42;
TemplatedBase<char> p;
p.foo(c);
}
--------------
g++ scratchpad.cpp scratchmod.cpp
/cygdrive/c/Temp/ccQbDL3c.o(.text+0x31):scratchpad.cpp: undefined
reference to `TemplatedBase<char>::foo(char)'
Is it a bug or a feature? And how to do it right?
Regards,
Michael