Extern templates


A template specialization can be explicitly declared as a way to suppress multiple instantiations. For example:

  1. #include "MyVector.h"
  2. extern template class MyVector<int>; // Suppresses implicit instantiation below --
  3. // MyVector<int> will be explicitly instantiated elsewhere
  4. void foo(MyVector<int>& v)
  5. {
  6. // use the vector in here
  7. }

The “elsewhere” might look something like this:

  1. #include "MyVector.h"
  2. template class MyVector<int>; // Make MyVector available to clients (e.g., of the shared library

This is basically a way of avoiding significant redundant work by the compiler and linker.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章