Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate c++ template instantiations

Tags:

c++

templates

Is it possible for the compiler to duplicate instantiations of a same template across several translation units?

For instance, if you have a.cpp which use a std:vector<int> inside a function, and same thing for b.cpp, is there a risk of having two times std::vector<int> in the final binary?

like image 244
Guillaume Paris Avatar asked Apr 04 '11 10:04

Guillaume Paris


1 Answers

This can happen while the project is being compiled, so different .obj files will have copies of the same instantiation. When the binary is linked the linker will eliminate all redundant copies of an instantiation, so the end binary will have only one copy.

like image 178
sharptooth Avatar answered Oct 11 '22 01:10

sharptooth