How to overload member function pointer and a regualr member function
Ali Çehreli via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Apr 25 11:54:01 PDT 2017
On 04/25/2017 11:28 AM, ParticlePeter wrote:
> On Tuesday, 25 April 2017 at 16:27:43 UTC, Basile B. wrote:
>> with pragma(inline, true), the function body should be injected at the
>> call sites.
>
> This would not help I fear, the body of the function pointer is unknown
> in an external lib. I rather hoped that the compiler "sees" the
> parameter forwarding to the fp and is able to directly call it. Best
> thing would be for both overloads, but I would not know how to verify
this.
pragma(inline, true) works because all you need inlined in this case is
the body of bar(int, float). The compiler does call the function pointer
directly.
import std.stdio;
struct Foo1
{
private void function(int,float) _bar;
void bar(float f) {
pragma(inline, true);
writefln("Called with %s", f);
}
void bar(int i, float f) {
pragma(inline, true);
_bar(i,f);
}
}
void func(int i, float f) {
writefln("Called with %s and %s", i, f);
}
void main() {
auto f = Foo1(&func);
f.bar(1.5);
f.bar(42, 2.5);
}
Compile with -inline (and perhaps with -O):
dmd -inline deneme.d
Generate the disassembly with obj2asm that comes with dmd (or with any
other disassembly tool):
obj2asm deneme.o > deneme.asm
You can open deneme.asm in an editor and search for function "_Dmain:"
in it. Here is what my dmd 2.074 produced:
_Dmain:
push RBP
mov RBP,RSP
sub RSP,010h
mov RAX,_D6deneme4funcFifZv at GOTPCREL[RIP]
mov -010h[RBP],RAX
movss XMM0,FLAT:.rodata[00h][RIP]
movss -8[RBP],XMM0
lea RDX,_TMP0 at PC32[RIP]
mov EDI,0Eh
mov RSI,RDX
movss XMM0,-8[RBP]
call _D3std5stdio17__T8writeflnTaTfZ8writeflnFNfxAafZv at PLT32
mov EAX,02Ah
movss XMM1,FLAT:.rodata[00h][RIP]
movss -4[RBP],XMM1
mov RDI,RAX
movss XMM0,-4[RBP]
call qword ptr -010h[RBP]
xor EAX,EAX
leave
ret
add [RAX],AL
.text._Dmain ends
The call to jumbled writefln() is a direct call inside func():
call _D3std5stdio17__T8writeflnTaTfZ8writeflnFNfxAafZv at PLT32
So, you're good... :)
Ali
More information about the Digitalmars-d-learn
mailing list