Given this code:
template <class T>
class Foo
{
public:
Foo(T t) : t(t) {}
T t;
};
#include "header.h"
extern template class Foo<int>;
int main()
{
Foo<int> f(42);
}
In my opinion, this program should not be linked, as there should be no definition class Foo<int>anywhere ( extern templateshould prevent this). With VC ++ 11 (Visual Studio 2012), however, this is compiled and linked. In GCC, this is not the case:
source1.cpp:(.text+0x15): undefined reference to `Foo<int>::Foo(int)'
If I refer to source2.cpp, then it works (as expected):
#include "header.h"
template class Foo<int>;
According to this blog post, the extern pattern should be supported with VC10.
http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx
On the side of the note, is there a way to list the names in an object file in Windows / Visual Studio? On Linux, I would do:
$ nm source1.o
U _ZN3FooIiEC1Ei <- "U" means that this symbol is undefined.
0000000000000000 T main
source
share