0 cost template instantiation

Hipreme msnmancini at hotmail.com
Thu Sep 30 01:09:47 UTC 2021


I have a template function that all it does is given a symbol, it 
loads a dll for its type + its name:

```
void loadSymbol(alias s, string symName = "")()
{
	static if(symName == "")
		s = cast(typeof(s))_loadSymbol(_dll, (s.stringof~"\0").ptr);
	else
		s = cast(typeof(s))_loadSymbol(_dll, (symName~"\0").ptr);
}
```


The main problem is that this function is costing 2KB per 
instantiation, which is something pretty high. Specially if I 
inline, there is almost no cost when compared to its inline 
version. Trying to use pragma(inline, true) didn't do anything 
too.

If I understood correctly, mixin template would be some way to 
actually inline anything. The problem is that I can't have any 
kind of expression inside it, so, that's the only way I could 
think in how to do it.

Optimization seems to don't take care of that behaviour too.

I would like to know which approach could I take for making 
something like C's #define for that.

As this function could be really rewritten as a macro if I were 
coding in C, the cost is too high for a function that would be 
only a syntactic sugar


More information about the Digitalmars-d-learn mailing list