When would you use template explicit instantiation?
c++, explicit-instantiation, templates
Solution
One of the use cases is to hide definitions from the end-user.
tpl.h:
template<typename T>
void func(); // Declaration
tpl.cpp:
template<typename T>
void func()
{
// Definition
}
template void func<int>(); // explicit instantiation for int
template void func<double>(); // explicit instantiation for double
main.cpp
#include "tpl.h"
int main()
{
func<double>(); // OK
func<int>(); // OK
// func<char>(); - Linking ERROR
}
Problem
I've just been reading about template explicit instantiation: `template struct MyStruct<long>;` It was described as "quite rare", so under what circumstances would it be useful?