I have a function which has exactly the same code for many basic types. To save code lines, I want to declare it as template and instantiate it explicitly for all later used types once: declaration in header file and implementation + explicit instantiation in cpp file (same design as writing normal function bibs).
My approach was:
// header.h
template <class T> T func (T arg);
// header.cpp
#include "header.h"
template <class T> T func (T arg)
{
    // implementation...
}
template int func<int> (int arg);
template double func<double> (double arg);
However, on parashift, I found this form of declaration:
// header.h
template <typename T> extern void foo();
What is the sense of extern here? How differs it from my approach? What is correct?
Also normal functions never have to be declared extern. It would be different if it was extern template void foo<int>(); (see accepted answer here), what would prohibit the compiler to instatiate a template that is already implemented in the header file.
Edit:
Is
// header.h
int func (int arg);
double func (double arg);
// header.cpp
#include "header.h"
int func (int arg)
{ ... }
double func (double arg)
{ ... }
not completely analogous/equivalent to
// header.h
template <class T> T func (T arg);
// here is the question: like above or template <class T> extern T func (T arg); ???
// header.cpp
#include "header.h"
template <class T> T func (T arg)
{ ... }
template int func<int> (int arg);
template double func<double> (double arg);
concerning later usage
// main.cpp
#include "header.h"
int main ()
{
    // for overloading
    func(20);
    func(20.0);
    func((int)20.0);
    // for template
    func(20); // == func<int>(20)
    func(20.0); // == func<double>(20.0)
    func((int)20.0); // == func<int>((int)20.0)
    func<int>(20.0); // == func<int>((int)20.0)
    return 0;
}
 
     
    