First: No, I am not asking about template specializations.
Assume the following example: I have a header file with a generic method:
template <typename T>
T foo(T bar) {
return bar;
}
In a second file, called file1.cpp
I use that method as foo<int>(42);
. Now as far as my understanding goes the compiler will generate the object code for the template method in the generated file1.o
object file.
Now if I have 10000 files which all use that method with the int template parameter, the compiler will generate the very same code 10000 times, as the object code generation is independent of all other objects (at least I think thats the case).
The question I have is: Once the linker combines all 10000 objects files into my binary, does he copy in the same code 10000 times, or can he detect this and only include the method once (per template type)?