*.cppファイルのVS12でそのようなものを使用します
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
別の*.cppで、一般的に使用されるすべてのテンプレートインスタンスをインスタンス化したい
#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>;
ベクトルが最初の*.cppファイルでインスタンス化されることを知る方法は、extern宣言を使用した場合と使用しない場合とで同じコンパイル時間を取得するためです(実際にはexternをより多くの型で宣言しているため、タイミングが違いを確認するのに十分な大きさであることがわかります)
質問:別の*.cppファイルでのみstd:: vectorをインスタンス化しないのはなぜですか?
同じ*.cppファイルにある場合は編集します
extern template class std::list<int>;
void somefunc()
{
std::list<int> v1;
}
別のcppファイルでstd::listを明示的にインスタンス化しなくても、likerエラーは発生しません。これは、externを指定しても、実際にはそれがインスタンス化されるという事実を説明できます。それはなぜだろうか。std::listを1つのポイントでインスタンス化したい。