If I compile the following code:
//
// g++ static.cpp -o static.o
// ar rcs libstatic.a static.o
//
#include <iostream>
template < typename T >
struct TemplatedClass
{
void Test( T value )
{
std::cout << "Foobar was: " << value << std::endl;
}
};
template struct TemplatedClass < long >;
I get a static library and if I run nm on the library I get the following results:
testcase% nm libstatic.a | c++filt | grep TemplatedClass
0000000000000207 s global constructors keyed to _ZN14TemplatedClassIlE4TestEl
0000000000000300 s global constructors keyed to _ZN14TemplatedClassIlE4TestEl.eh
0000000000000118 T TemplatedClass<long>::Test(long)
00000000000002a0 S __ZN14TemplatedClassIlE4TestEl.eh
If however I compile the following code, which is the same except that I have added an explicit specialization of the templated class...
//
// g++ static.cpp -o static.o
// ar rcs libstatic.a static.o
//
#include <iostream>
template < typename T >
struct TemplatedClass
{
void Test( T value )
{
std::cout << "Foobar was: " << value << std::endl;
}
};
template <>
struct TemplatedClass < long >
{
void Test( long value )
{
std::cout << "Value was: " << value << std::endl;
}
};
template struct TemplatedClass < long >;
... and rerun the same command:
testcase% nm libstatic.a | c++filt| grep TemplatedClass
testcase%
I get no matching symbols. For some reason the compiler does not instantiate the template even though I explicitly asked it to.
Can someone explain to me what is going on here?