I read that templates are complied into different entities so does that mean the binary size will be same as we have complied it using different functions?
Class templates are generally used to implement containers. A class template is instantiated by passing a given set of types to it as template arguments. The C++ Standard Library contains many class templates, in particular the containers adapted from the Standard Template Library, such as vector .
A template is a C++ entity that defines one of the following: a family of classes (class template), which may be nested classes. a family of functions (function template), which may be member functions.
They should shorten the source size (if they are reused) but not the binary size (the template is compiled for each different instantiation).
This differs from Java generics, where there is a full type erasure (generics only serve as a compile time verification of types) or C#, where generics are compiled into specific binaries that can be directly reused without having to recompile and generate more code.
My understanding is that for each type you instantiate with a template the complier produces the relevant class to match that type - so if you use List<int>
, List<foo>
and List<float>
there will effectively be three different List
classes in your complied binary.
Edit:
What I didn't explicitly state was that I'm inferring that merging several classes in to a single template will (probably) not reduce the size of your binary, but should reduce the size of your source.
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