How are (Static) Libraries with Templates Compiled?

H. S. Teoh hsteoh at quickfur.ath.cx
Mon Mar 11 20:18:14 UTC 2019


On Mon, Mar 11, 2019 at 07:53:53PM +0000, jmh530 via Digitalmars-d-learn wrote:
> Suppose I have a file with a simple templated function. I compile that
> to a static library. I then compile another file that uses the library
> into an executable. When compiled to the static library, you don't
> know in advance what types it will call on the templated function, so
> that must be resolved when compiling the executable. So what
> information is in the static library that allows this to take place?
> Is the library just a simple collection of object files?

Templates are never compiled into any object code.  Only template
instantiations are.

This is why you cannot elide the template body from a .di file --
because in order to instantiate the template, the compiler must know the
full definition of the template.  Unlike functions, where you can just
declare the function signature, and the body can be an opaque binary
blob that's only supplied in a precompiled object/library file.


T

-- 
Famous last words: I wonder what will happen if I do *this*...


More information about the Digitalmars-d-learn mailing list