Phobos: __FILE__ as template default parameter

Lass Safin via Digitalmars-d digitalmars-d at puremagic.com
Sun Jun 19 15:20:48 PDT 2016


On Saturday, 18 June 2016 at 18:13:22 UTC, Johan Engelen wrote:
> 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

Can't one just use __MODULE__ instead?

So:
T file_templ(T, string mod = __MODULE__, size_t line = __LINE__)
(T value) {
     return value;
}



More information about the Digitalmars-d mailing list