Template - code bloat ?!!
-
-
I am new to templates in C++. Say I have declared Vector in first.cpp and also in Second.cpp. Does the compiler generate two Vector implementation ?. If so is there any way I can force the compiler to generate just a single implementation. Thanks
It is the linker's responsibility to strip off duplicates across compilation units, so in general there'll be only one implementation. Code bloat can stem from another reasons:
- instantiating
vector
s (or other template classes) of many different types - code bloat resulting from massive inlining
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
- instantiating
-
It works exactly the same as if you had two instances of a non-templated class. The template code bloat is that the compiler needs to generate new classes for each DIFFERENT template parameter it is used with. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001 Picture the daffodil. And while you do that, I'll be over here going through your stuff.
-
I am new to templates in C++. Say I have declared Vector in first.cpp and also in Second.cpp. Does the compiler generate two Vector implementation ?. If so is there any way I can force the compiler to generate just a single implementation. Thanks
It works exactly the same as if you had two instances of a non-templated class. The template code bloat is that the compiler needs to generate new classes for each DIFFERENT template parameter it is used with. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001 Picture the daffodil. And while you do that, I'll be over here going through your stuff.
-
It is the linker's responsibility to strip off duplicates across compilation units, so in general there'll be only one implementation. Code bloat can stem from another reasons:
- instantiating
vector
s (or other template classes) of many different types - code bloat resulting from massive inlining
Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
Joaquín M López Muñoz wrote: code bloat resulting from massive inlining Although I'll point out that inline is a suggestion to the compiler, not a command. If you inline stupidly, the compiler will ignore you. Christian I have come to clean zee pooollll. - Michael Martin Dec 30, 2001 Picture the daffodil. And while you do that, I'll be over here going through your stuff.
- instantiating