Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does template cause the code bloat in C++?

Tags:

c++

templates

I never understood this issue with templates. For me instantiating a method multiple types for different type of parameter is same as implementing the same in terms of function overloading. If this is the case how template causes the code bloat or exceeds the binary size to a certain limit. Please bring clarity into this.

Sometimes I am unsure whether to use templates or function overloading. Template code bloat is the problem I have heard of, but never understood.

like image 594
Chris Avatar asked Jun 20 '14 18:06

Chris


Video Answer


1 Answers

How does template cause the code bloat in C++?

Code bloat occurs because compilers generate code for all templated functions in each translation unit that uses them. Back in the day, the duplicate code was not consolidated, which resulted in "code bloat". These days, the duplicate code can be removed at link time.

like image 82
Captain Obvlious Avatar answered Oct 12 '22 03:10

Captain Obvlious