Standard says that template names have external linkage.
My question is: "Why isn't there a linker error when I declare the same templates in two .cpp files and compile this files together?"
Global functions names have external linkage like templates names. That's why I suggest that they should have similar behavior. But as for functions there is nothing strange and the linker produces an error, but as for templates somehow there is no error.
temp_1.cpp
#include <iostream>
// error because of external linkage
// void eq_f(){}
template<class T>
void my_func() {
std::cout << "1!";
}
temp_2.cpp
#include <iostream>
// external linkage
// void eq_f(){}
template<class T>
void my_func() {
std::cout << "2!";
}
int main() {
my_func<int>();
return 0;
}
command:
clang++ temp_1.cpp temp_2.cpp -std=c++17