When you use a template with numerous methods (like vector) and compile your code, will the compiler discard the code from the unused methods?
-
1That depends on a lot of thing: the particular compiler/linker, the optimization level, whether you're compiling an executable/library/DLL, etc. – Adam Rosenfield Aug 09 '12 at 20:00
-
Are we to assume that "unused methods" means methods that are never called anywhere in the source code? Or are we to assume you're looking for functions that never get called in the life of the program? – Dennis Meng Aug 09 '12 at 20:00
-
1Note that member functions of class templates don't actually get compiled until they are used anyway. (Only the first pass occurs, the second occurs on use.) – GManNickG Aug 09 '12 at 20:05
-
@GManNickG what do you mean by that? – ecatmur Aug 09 '12 at 20:09
-
@DennisMeng Methods that are never called in the source code. – ixron Aug 09 '12 at 20:43
-
Okay, just making 100% sure. I think the latter would run into Halting Problem issues. – Dennis Meng Aug 09 '12 at 20:45
-
@ecatmur: Given `std::vector
v;`, `std::vector – GManNickG Aug 09 '12 at 21:10::clear` only goes through the first phase of template compilation (and generates nothing the linker will see). It isn't until `v.clear();` is compiled that the function goes through the second phase, and the compiler actuallly generates any code. -
@GManNickG you mean instantiation of member functions? – ecatmur Aug 09 '12 at 21:26
4 Answers
A template is not instantiated unless it is used, so there is actually no code to discard.
The standard says (14.7.1/10)
An implementation shall not implicitly instantiate a function template, a member template, a non-virtual member function, a member class, or a static data member of a class template that does not require instantiation. It is unspecified whether or not an implementation implicitly instantiates a virtual member function of a class template if the virtual member function would not otherwise be instantiated. The use of a template specialization in a default argument shall not cause the template to be implicitly instantiated except that a class template may be instantiated where its complete type is needed to determine the correctness of the default argument. The use of a default argument in a function call causes specializations in the default argument to be implicitly instantiated.
So if you can avoid making the template's member functions virtual, the compiler will not generate any code for them (and that might work for virtual functions as well, if the compiler is smart enough).

- 90,663
- 31
- 146
- 203
It depends on your optimization level. At higher optimization settings, yes, dead code elimination will most likely occur.
the compiler, optimizers, and the linker can omit and/or reduce that information. each mature tool likely has options specific to dead code elimination.
with templates, the code may not really be created in the first place (unless instantiated).
certainly not all of it will be removed in every scenario, however (rtti is a silent killer). a bit of caution and testing using your build settings can go a long way to help you reduce the binary sizes and dead code.

- 104,054
- 14
- 179
- 226
Smart compilers will exclude it most likely. Long time ago when I played with Borland C++ Builder, I think, it did not throw out unused template class methods. Can not confirm though

- 2,544
- 1
- 21
- 20