I know the one definition rule and why the function definition should not be put in the header file if you want to use it in multiple source files.
However, I don't quite get the reason why we can put the function template in the header file. During the compile time, the compiler creates an instantiation. Now aren't there still two function definitions for "int sum(int, int)" for each translation unit as the example below.
// main.cpp
include "helper.hpp"
int main() {
sum(2, 3);
return 0;
}
// helper.hpp
template <typename T>
T sum(T a, T b) {
return a + b;
}
// helper.cpp
#include "helper.hpp"
int sum2(int a, int b)
{
return sum(a, b);
}