Mangling template

Wusiki jeronii wusikijeronii at gmail.com
Fri May 7 06:17:32 UTC 2021


On Friday, 7 May 2021 at 05:58:50 UTC, evilrat wrote:
> On Thursday, 6 May 2021 at 22:03:36 UTC, Wusiki jeronii wrote:
>
> Template parameters is compile time construct, compiler only 
> emits code for template instances and not the template itself, 
> so template declaration is simply does not exist in compiled 
> code.
>
> Templates ends up as regular functions, the real deal is 
> getting that name to load from .so, it is possible to load it 
> with ldsym or any other mechanism.
>
> You can probably adapt mangle function from std library by 
> copying its implementation and rework it to make regular 
> function that combines everything using strings. That will take 
> quite some time and effort though.
>
> https://dlang.org/phobos/core_demangle.html#.mangle
>
>
> Or, you can just apply some mixin magic locally to construct 
> some kind of interface tables with mapping from regular 
> template name that will give you function pointer for specific 
> instance.

I understand that. Finally, I've got the right reference to 
functions.
Library:
```d
     template test(T)
     {
         pragma(mangle, mangleFunc!(T function(T))("dll.test")) T 
test(T a)
         {
             return a;
         }
     }
```
Library loader:
```d
     template abcd(T)
     {
         T function(T) abcd;
     }

     static this()
     {
         libdll = Library("libs/libdll.so");
         template abcd(T)
         {
             T function(T) abcd = cast(T function(T)) 
libdll.loadSymbol!(T function(T))("dll.test");
         }
         writeln(abcd!int(9));
     }

static ~this()
{

         version (Windows)
             Runtime.unloadLibrary(libdll.handle);
         else
             dlclose(libdll.handle);
         libdll.handle = null;
     }
```
I get the error:
Error: static variable `libdll` cannot be read at compile time.

How to redefine template? Need to point all functions by type 
manually without a template?


More information about the Digitalmars-d mailing list