www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Compiling a template

reply albertas-jn <adasf fsdk.com> writes:
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?
Dec 06 2018
next sibling parent Basile B. <b2.temp gmx.com> writes:
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.
Dec 06 2018
prev sibling next sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Dec 06 2018
parent albertas-jn <adasf fsdk.com> writes:
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.
Dec 06 2018
prev sibling parent Neia Neutuladh <neia ikeran.org> writes:
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.
Dec 06 2018