Can anyone provide an example of how D templates are overridable by global symbols?

Siarhei Siamashka siarhei.siamashka at gmail.com
Thu Dec 9 21:06:54 UTC 2021


On Thursday, 9 December 2021 at 20:53:52 UTC, Siarhei Siamashka 
wrote:
>   2. How would one construct a simple example of a template 
> symbol getting successfully overridden by a global symbol?

Here's my unsuccessful attempt:

```D
module template_f;

T f(T)(T a, T b) { return a + b; }
```

```D
module nontemplate_f;

int f(int a, int b) { return a - b; }
```

```D
import std.stdio;

import template_f;
import nontemplate_f;

void main()
{
   f(2, 1).writeln;
}

```

```text
$ gdc-10.3.0 test.d
test.d:8:4: error: nontemplate_f.f at nontemplate_f.d:3:5 
conflicts with template_f.f!int.f at template_f.d:3:3
     8 |   f(2, 1).writeln;
       |    ^
```
This is prohibited at the compilation stage and doesn't even 
reach the linker.

I guess, something a bit more elaborate needs to be done to 
simulate a global symbol from a rogue object file overriding a 
template from phobos in the resulting compiled binary. The 
question is how to achieve this.


More information about the Digitalmars-d-learn mailing list