Consider the following example:
fnc.h:
#include <iostream>
template <class T> //common template definition
void fnc()
{
std::cout << "common";
}
fnc.cpp:
#include "fnc.h"
template<> //specialization for "int"
void fnc<int>()
{
std::cout << "int";
}
main.cpp
#include "fnc.h"
extern template void fnc<int>(); // compiler will not generate fnc<int> and linker will be used to find "fnc<int>" in other object files
int main()
{
fnc<double>(); //using instantiation from common template
fnc<int>(); using specialization from "fnc.cpp"
}
Then I replace extern template void fnc<int>();
with template<> void fnc<int>();
and assume the behavior will be same. Is there any real meaning to using the extern
keyword with templates or was it introduced for readability only?