Thread overview
Compiling a template
Dec 06, 2018
albertas-jn
Dec 06, 2018
Basile B.
Dec 07, 2018
Ali Çehreli
Dec 07, 2018
albertas-jn
Dec 07, 2018
Neia Neutuladh
December 06, 2018
If templates are a compile-time feature and instances of templates are generated by compiler at compile time, why is it possible to compile a template definition with dmd -lib or -c?
December 06, 2018
On Thursday, 6 December 2018 at 22:50:49 UTC, albertas-jn wrote:
> If templates are a compile-time feature and instances of templates are generated by compiler at compile time, why is it possible to compile a template definition with dmd -lib or -c?

Because to instantiate the source code is still used (-I). Just the D interface is even not enough.
December 06, 2018
On 12/06/2018 02:50 PM, albertas-jn wrote:
> If templates are a compile-time feature and instances of templates are generated by compiler at compile time, why is it possible to compile a template definition with dmd -lib or -c?

There is no trace of the template in the library or the object file. You can investigate the compiled symbols with e.g. the 'nm' tool on Linux systems:

// deneme.d:
void foo(T)(T t) {
    import std.stdio;
    writeln(t);
}

void main() {
    // foo(42);
}

$ dmd deneme.d -lib
$ nm deneme.a | grep foo

No trace of foo... Now uncomment the line in main and repeat:

$ dmd deneme.d -lib
$ nm deneme.a | grep foo
                 U _D6deneme__T3fooTiZQhFNfiZv
0000000000000000 W _D6deneme__T3fooTiZQhFNfiZv

"W" indicates a definition.

Ali
December 07, 2018
On Thu, 06 Dec 2018 22:50:49 +0000, albertas-jn wrote:
> If templates are a compile-time feature and instances of templates are generated by compiler at compile time, why is it possible to compile a template definition with dmd -lib or -c?

You compile files, not individual declarations like a template. If you have a source file containing a hundred templates and nothing else, and you compile it, you'll get the same output as if you had an empty source file, byte for byte.
December 07, 2018
On Friday, 7 December 2018 at 01:21:42 UTC, Ali Çehreli wrote:

> There is no trace of the template in the library or the object file. You can investigate the compiled symbols with e.g. the 'nm' tool on Linux systems:
>
> // deneme.d:
> void foo(T)(T t) {
>     import std.stdio;
>     writeln(t);
> }
>
> void main() {
>     // foo(42);
> }
>
> $ dmd deneme.d -lib
> $ nm deneme.a | grep foo
>
> No trace of foo... Now uncomment the line in main and repeat:
>
> $ dmd deneme.d -lib
> $ nm deneme.a | grep foo
>                  U _D6deneme__T3fooTiZQhFNfiZv
> 0000000000000000 W _D6deneme__T3fooTiZQhFNfiZv
>
> "W" indicates a definition.

I see, what confused me was that if I put main() in a different file and

$ dmd main.d deneme.a

the program compiled properly. Now I realize that in this case deneme.a file was ignored and the source file was used instead. I expected an error. Thank you for your answers.