Inclusion Model (implicit instantiations)

// header.h
#include<iostream>

template<class T>
struct MyClass {
   MyClass(T m) : _m(m){}
   void show(){ std::cout << _m << "\n"; }
   T _m;
};
// foo.cc
# include "header.h"

void foo(){
   MyClass<double> objd(-5.2);
   objd.show();

   MyClass<int> obji(-5);
   obji.show();
}
// main.cc
# include "header.h"

void foo();

int main(){
   MyClass<int> obj(1);
   obj.show();
   foo();
   return 0;
}

image

Exclusion Model (explicit instantiations )

// header.h
template<class T, int size, int weight=1>
struct MyClass {
   MyClass(T m);
   void show();
   T _m;
};
// header.cc
#include<iostream>
#include"header.h"

template<class T, int size, int weight>
MyClass<T,size,weight>::MyClass(T m) : _m(m){ }

template<class T, int size, int weight>
void MyClass<T,size,weight>::show(){
   std::cout << _m << "\n";
}

template class MyClass<int, 10, 10>;
template class MyClass<double, 10>;
// foo.cc
# include "header.h"

void foo(){
   MyClass<double,10> objd(-5.2);
   objd.show();

   MyClass<int,10,10> obji(-5);
   obji.show();
}
// main.cc
#include "header.h"

void foo();

int main(){
   MyClass<int,10,10> obj(1);
   obj.show();
   foo();
   return 0;
}

image

Exclusion Model (extern template)

// header.h
#include<iostream>

template<class T>
struct MyClass {
   MyClass(T m) : _m(m){}
   void show(){ std::cout << _m << "\n"; }
   T _m;
};
// foo.cc
# include "header.h"

void foo(){
   MyClass<double> objd(-5.2);
   objd.show();

   MyClass<int> obji(-5);
   obji.show();
}
// main.cc
#include "header.h"

void foo();
extern template class MyClass<int>;

int main(){
   MyClass<int> obj(1);
   obj.show();
   foo();
   return 0;
}

image