Is there an error with the extern pattern in Visual C ++?

Given this code:

//header.h
template <class T>
class Foo
{
public:
  Foo(T t) : t(t) {}
  T t;
};

//source1.cpp:
#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
+5
source share
1 answer

++ 11 14.7.2/10 " " :

, , .

Foo<T> . VS2012 , , :

//header.h
template <class T>
class Foo
{
public:
  Foo(T t);
  T t;
};

template <class T>
Foo<T>::Foo(T t) : t(t) 
{
}

.

:

[. , , - odr (3.2), , . - ]

, , ctor , ctor ( ctor , ) MSVC, , . , , MSVC .


, MSVC, dumpbin:

:

dumpbin /symbols test.obj

...

008 00000000 UNDEF  notype ()    External     | ??0?$Foo@H@@QAE@H@Z (public: __thiscall Foo<int>::Foo<int>(int))
             ^^^^^
...

ctor inlined:

00A 00000000 SECT4  notype ()    External     | ??0?$Foo@H@@QAE@H@Z (public: __thiscall Foo<int>::Foo<int>(int))
             ^^^^^
+14

All Articles