Extern templates

来源:互联网 发布:windows 10 dpi 编辑:程序博客网 时间:2024/06/08 17:39


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.

0 0