Compiler Explorer: GDC 15.2 '-O2 -fno-moduleinfo'
Source:
int pass(int a) { return a; } //normal function
int add(int num) { return pass(num) + num;}
Output:
int example.pass(int):
mov eax, edi
ret
int example.add(int):
lea eax, [rdi+rdi]
ret
as expected. Now changing 'pass' to template function
int pass()(int a) { return a; } //template function
int add(int num) { return pass(num) + num;}
Output:
pure nothrow @nogc @safe int example.pass!().pass(int):
mov eax, edi
ret
int example.add(int):
push rbx
mov ebx, edi
call pure nothrow @nogc @safe int example.pass!().pass(int)
add eax, ebx
pop rbx
ret
way worst output.
Lets force inlining template function
pragma(inline,true)
int pass()(int a) { return a; } //template function (force inline)
int add(int num) { return pass(num) + num;}
Output:
int example.add(int):
lea eax, [rdi+rdi]
ret
Now we get expected output.
But lack of inlining is not the only problem because
pragma(inline,false)
int pass(int a) { return a; } //normal function (no inline)
int add(int num) { return pass(num) + num;}
Output:
int example.pass(int):
mov eax, edi
ret
int example.add(int):
call int example.pass(int)
add eax, edi
ret
which is still better output than templated one.
I checked multiple version GDC on 'Compiler Explorer' and last version you get expected output for templated functions is GDC 10.5
My local GDC(15.2.1) installation give same result as 'Compiler Explorer' one.
Tested c++ equivalent code and got expected result