0.cc
template <class T>
T get(){
return 5;
}
int get(){
return 6;
}
int main(){
return get<int>();
}
1.cc
template <class T>
T get(){
return 7;
}
template int get<int>(); // This forces code generation.
Compiling with g++ -Wall 0.cc 1.cc
causes no link errors, returned output is 5
.
Questions
1- Do templates have external linkage by default even if extern
isn't used?
https://en.cppreference.com/w/cpp/language/storage_duration
names of all templates not listed above (that is, not function templates declared static).
2- Does the linker treat multiple templates like inline
functions? i.e it chooses 1 out of many definitions and having different definitions causes UB?
https://stackoverflow.com/a/66356946
3- Why doesn't int get(){}
cause a link error? do template functions and regular functions have different symbols?