When I include, say <vector>
in a C++ file, and create an instance of std::vector<double>
, and then call the std::vector<double>::push_back()
function, my understanding is that at that point, the compiler creates a definition of the function and generates the necessary code. However, I can call that function from several distinct C++ files which will be linked into the same executable, and presumably each .cpp file will generate a .o file each it's own definition of std::vector<double>::push_back()
. Normally, that kind of multiple function definition would generate a linking error, but it doesn't seem to happen for templates- why is that so?

- 294
- 1
- 9
1 Answers
Compilers can flag certain symbols to be autoremoved in case the linker sees redefinition.
The "symbol" in this case will be the unique name (the mangled name) that the compiler assigns to each templated function, or templated member function, or member function of a templated class, after instantiation with a concrete type. In different translation units (different .cpp
), the generated unique names will be exactly the same. Consider that, besides exceptions and polimorphic hierarchies, a C++ program is basically translated to a C program before applying the final translation to assembler code, so your program get up being a bunch of functions, some of them being inline functions, others instantiated templated functions, and others usual free functions. All but the thirds are flagged by the compiler to let the linker know redefinitions are possible comming and they must be silently removed.
If a different translation unit uses a same templated method instantiated with a same type, the mangled name will also be the same, and then flagged by the compiler. Finally, in the linking stage, the linker will see two redefinitions of a same function, but flagged; so the linker must remove one of them and only one will survice the purge.
It's the same with inline methods.
This causes no harm since a same method from a same instiantated templated class for a same type, will produce the exact same assembler code, no matter from which translation unit.
Otherwise it would be impossible to have inline or templated methods.
AFAIK, originally linkers didn't allow this kind of flagging; it was explicitely added to make C++ implementable.

- 10,423
- 6
- 52
- 103
-
Sure but then why is this behavior different for non-template functions. The compiler could flag these cases of multiple definition for auto-removal by the linker, but it doesn't. The question is why? – FreelanceConsultant Jul 27 '22 at 13:17
-
This question answers my question posted above: https://stackoverflow.com/questions/44335046/how-does-the-linker-handle-identical-template-instantiations-across-translation – FreelanceConsultant Jul 27 '22 at 13:26
-
@FreelanceConsultant I guess because it would be error prone. What If you repeat a symbol name by mistake? Also, remember that strong linking comes from C where there's no notion of namespaces, but I guess the defaulted strong linking was kept on the C++ standard maybe for the reason stated above – ABu Jul 27 '22 at 15:54