Phobos: __FILE__ as template default parameter

Johan Engelen via Digitalmars-d digitalmars-d at puremagic.com
Sat Jun 18 11:13:22 PDT 2016


An example of how __FILE__ as template parameter will break your 
library:

In library, distributed in binary+source form:
```
alias file_templ_alias = file_templ!bool;
T file_templ(T, string file = __FILE__, size_t line = __LINE__) 
(T value) {
     return value;
}
```
In user code, linking to the library binary:
```
bool foo(bool b){
     return file_templ_alias(b);
}
```
By calling the alias, both DMD and LDC will decide that the 
template does not need reinstantiation in the user object file 
and will try to link with the symbol in the library binary. So 
you have to be lucky to install the library source in the same 
path as where it was when the library was built on someone else's 
machine, otherwise you'll get a linker error and are left 
wondering what went wrong.

-Johan


More information about the Digitalmars-d mailing list