Let's say I declare a template but never reference it in C++. Does the compiler do anything with it in the executable? Does it take up any process memory?
No, no code will be produced for a function template which is never instantiated. Also, no code will be produced for a non-template member function of a class template which is never called.
In particular, the size of your executable won't be any bigger, nor will any run-time overhead occur.
No it does not.
Template classes and functions are not actual classes or functions: They are instead directions to the compiler for how to generate certain types of classes and functions. When you reference a template, the compiler, uses the template in order to figure out how to generate the source code, and compiles the generated code, once for each different parameterization of the template.
The only overhead to using a template is a little extra compile time, no big deal for any computer built anytime after the 1980s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With