For example, consider the following code:
// f.h
template <typename T>
int f(T x) {
return x+1;
}
If we instantiate it in both foo.cpp
and bar.cpp
:
//foo.cpp instantiation:
int i = f(1);
and
//bar.cpp instantiation:
int j = f(2);
Will the final program uses only one copy of code? Is that so, when bar.cpp
is hidden but only the object file bar.o
is provided?
I think, since each cpp files are compiled independently, both foo.o
and bar.o
must contains f(int)
. The linker should see same duplicate signature, and use only one copy. Am I right?