www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Re: templated()

Ary Borenszweig:

 Can't the compiler see what is used and where?

In practice: today I think DMD is not able to do it. LDC has a pass to merge equal or very similar functions, but it's experimental and not much tested yet (and on default it's disabled), and it has strong limitations, it works only in few situations. I don't know about GCC, but probably it too has some limitations. A problem: once the template is instantiated the various functions need different to have different pointers. To face this problem LDC leaves a tiny stub for function duplicates. This problem is not present with templated(), because the annotation allows to change the semantics too a little, so those stubs are not needed. Another problem: templated() avoids some template bloat not duplicating the code. The duplicate removal feature of a compiler like LDC is something extra that removes something that has being compiled, etc. So templated() allows for a faster compilation, unless the compiler is smarter and doesn't even generate the duplicated functions. templated() is a contract between programmer and compiler. The programmer states that she doesn't want to use X and Y template arguments in a part of the template, and the compiler enforces this. So it's enforced documentation that allows the program to specify a bit more semantics in an explicit way. Compiler optimizations are something different. Something good: templated() with no arguments allows to have code that is not templated inside a templated struct, class, etc. This allows to create compiled libraries that contain more code. Bye, bearophile
Jan 13 2011