How do I explicitly instantiate a template function?

[EDIT 2]: Note that there was some confusion regarding the code in the original question due to code formatting issues. See AnthonyHatchkins’ answer for more details.

If you really want to instantiate (instead of specialize or something) the function, do this:

template <typename T> void func(T param) {} // definition

template void func<int>(int param); // explicit instantiation.

[EDIT] There seems to be (a lot) of confusion regarding explicit instantiation and specialization.
The code I posted above deals with explicit instantiation. The syntax for specialization is different.
Here is syntax for specialization:

template <typename T> void func(T param) {} // definition

template <> void func<int>(int param) {} // specialization

Note that angle brackets after template!

Leave a Comment