Mangling template

evilrat evilrat666 at gmail.com
Fri May 7 05:58:50 UTC 2021


On Thursday, 6 May 2021 at 22:03:36 UTC, Wusiki jeronii wrote:
> I wanna import a template function from .so lib. .so is written 
> in D.
> ```d
> T abcd(T)(T a)
> {
>     return a;
> }
>
> ........
>
> writeln(abcd!int.mangleof);
> auto a = mangle!(typeof(abcd!int))("dll.abcd");
> writeln(a);
> ```
> Output:
> _D3dll__T4abcdTiZQiFNaNbNiNfiZi
> _D3dll4abcdFNaNbNiNfiZi
>
> Demangled:
> pure nothrow @nogc @safe int dll.abcd!(int).abcd(int)
> pure nothrow @nogc @safe int dll.abcd(int)
>
> How to pass the template part to the mangle template? Is it 
> possible at all to import a template function?

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.



More information about the Digitalmars-d mailing list