I once read somewhere that C++ essentially enumerates all of the possible template types based on usage at compile time so the concept of template doesn't exist during runtime. It also seems that's what the accepted answer is from Template Compilation
My question is, if this is the case, how does STL handle custom types, when everything is compiled and done already? This could more broadly apply to any custom library that's compiled with templates.
(I could have asked this in the answer's comment but I don't have enough points)
Shared libraries (also called dynamic libraries) are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries.
Compiler creates a new instance of a template function for every data type. So compiler creates two functions in the above example, one for int and other for double. Every instance has its own copy of static variable. The int instance of function is called twice, so count is incremented for the second call.
Instantiation is the process by which a C++ compiler creates a usable function or object from a template. The C++ compiler uses compile-time instantiation, which forces instantiations to occur when the reference to the template is being compiled.
C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.
If a library wants to provide templates which its clients can instantiate with arbitrary types, it must provide the templates' complete definitions in header files. This is why a lot of C++ libraries, including most of Boost, are header-only. The compiler then has access to the template's definition and can instantiate it with any types/values the client provides as template arguments.
For a detailed treatment of the topic, please refer to the Stack Overflow question Why can templates only be implemented in the header file?.
Note that this only applies if, as I said, the templates are intended for use with arbitrary types. If the set of instantiations is limited and can be determined at the time the shared library is built, the library can create explicit instantiations of all the templates it wants all combinations of template arguments it wants to support. Then, exposing the definitions of the templates is not necessary, but of course, it will not be possible to instantiate the templates with different types in client code.
As an example of this, there are some geometric libraries which provide their definitions as templates so that they can work with both float
and double
to represent floating-point numbers, but do not expose the template definitions; they simply pre-instantiate all their code with float
and double
. Clients can then use these instantiations, but cannot use them with for exmaple long double
or MyCustomFloat
.
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