0

I've seen people who have a template class declaration in a header file and the implementation in a .cpp file, and in the .cpp file they implement the methods, and then it appears they declare some template specifications for those functions, e.g., the below code does it:

Class declaration file:

template<int dim = 2>
struct custom_class {
  void apply(int i, int j, int k);
}

Implementation file:

template <int dim>
void custom_class<dim>::apply() {
  // implement apply
}

// what is the point of the below two lines?
template void custom_class<2>::apply(int i, int j, int k);
template void custom_class<3>::apply(int i, int j, int k);

I am wondering what is the point of the last 2 lines of this code? What does it do?

user5965026
  • 465
  • 5
  • 16
  • 1
    The shown lines are incorrect. You seem to be missing a `template` keyword before each of them. With that, these become _explicit instantiations_, not _specializations_. – user17732522 Mar 02 '22 at 16:41
  • 1. Templates can be _instantiated_. 2. The two lines you ask about are _explicitly instantiating_ a template. – Drew Dormann Mar 02 '22 at 16:45

0 Answers0