I know that the following code will result in a linking error:
//first.cpp
void test(){
//random code
}
//second.cpp
void test(){
//random code
}
so lets say that we have this function template:
template<typename T>
T test(){
//random code
}
and are doing this:
//first.cpp
...
test<void>();
//second.cpp
...
test<void>();
so the way I understand how compiler works is that it only cares for each file individually so it only cares that test<void>()
must have a definition therefore it will create one. same thing goes for the second.cpp
then why don't we get an linker error at later stage when there are two definitions for test<void>
.(I think this should be same as the first example where those two void test()
functions in separate files lead to a linking error)
if this is a duplicate I'm sorry I literally didn't know how to search for this.