1

Possible Duplicate:
Is there a bug with extern template in Visual C++?

I use something like that in VS12 in a *.cpp file

    extern template class std::vector<int>;
... at some point here I need to instantiate vector on int
    std::vector<int> v1; // would not expect this to be instantiated here as I already have a *.cpp where I instantiate this *only once* for all project uses

In a separate *.cpp I want to instantiate all commonly used template instances

#include "stdafx.h"

template class std::vector<int>;
template class std::list<int>;
template class std::list<long>;
template class std::list<short>;
template class std::list<long long>;
template class std::list<unsigned int>;

The way I know vector is instantiated in first *.cpp file also is because I get same compile times with extern declaration as without it(I actually declare extern more types so I can be sure timings are large enough to see the difference)

Question: Why it does not instantiate std::vector only in the separate *.cpp file ?

EDIT If in the same *.cpp file I have

extern template class std::list<int>;

void somefunc()
{
   std::list<int> v1;
}

I do not get a liker error even if I don't explicitly instantiate std::list in a different cpp file. This could explain the fact that actually that gets instantiated even if I specify extern. Wonder why is that. I want to instantiate std::list in a single point.

Community
  • 1
  • 1
Gmt
  • 569
  • 1
  • 5
  • 19
  • @PeteBecker thanks for point this out. Corrected :-) – Gmt Nov 28 '12 at 21:01
  • Visual C++? Probably a compiler bug. – Xeo Nov 28 '12 at 21:12
  • @bames53 Yes, using a simple template class where things get defined outside of class declaration I get linker errors as expected. I have hard time using a method of list for example that is not inlined (defined when declaration is done) so that to see the linker error in that case also. You can post an answer to get credit. tnx. – Gmt Nov 28 '12 at 21:43

0 Answers0