How to overload member function pointer and a regualr member function

ParticlePeter via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Apr 26 20:17:32 PDT 2017


On Wednesday, 26 April 2017 at 08:24:08 UTC, Basile B. wrote:
> On Tuesday, 25 April 2017 at 18:58:58 UTC, Ali Çehreli wrote:
>> On 04/25/2017 11:54 AM, Ali Çehreli wrote:
>> My analysis is wrong because that writefln() is for the 
>> bar(float) overload but I still think what you want is 
>> achieved.
>>
>> Ali
>
> No it's ok, it works. The additional indirection is well 
> avoided:
>
> Let's take this module:
>
> ==================
> #!dmd -release -inline -O
> module runnable;
>
> struct Foo
> {
>     private void function(int,float) _bar;
>     void bar(float){}
>     pragma(inline, false) void bar(int i, float f){_bar(i,f);}
> }
>
> struct FooInline
> {
>     private void function(int,float) _bar;
>     void bar(float){}
>     pragma(inline, true) void bar(int i, float f){_bar(i,f);}
> }
>
> void testInlined(ref FooInline foo)
> {
>     foo.bar(0,0);
> }
>
> void test(ref Foo foo)
> {
>     foo.bar(0,0);
> }
>
> void main()
> {
>     import disassembler, std.stdio;
>     disassembler.symbolTable.addModule!runnable;
>     prettyDisasm(&testInlined).writeln;
>     prettyDisasm(&test, 2).writeln; // dig up to 2 levels, 
> required for the indir.
> }
> ==================
>
> and looks at the output:
>
>
> ;------- SUB 0000000000459970h -------
> ; NAMED: testInlined
> 0000000000459970h  push rbp
> 0000000000459971h  mov rbp, rsp
> 0000000000459974h  sub rsp, 20h
> 0000000000459978h  mov qword ptr [rbp-08h], rdi
> 000000000045997Ch  xor edi, edi
> 000000000045997Eh  mov dword ptr [rbp-20h], edi
> 0000000000459981h  movss xmm0, dword ptr [rbp-20h]
> 0000000000459986h  mov rax, qword ptr [rbp-08h]
> 000000000045998Ah  call qword ptr [rax]
> 000000000045998Dh  mov rsp, rbp
> 0000000000459990h  pop rbp
> 0000000000459991h  ret
> ;-------------------------------------
>
>
> ;------- SUB 0000000000459934h -------
> ; XREFS: [00000000004599A6h]
> 0000000000459934h  push rbp
> 0000000000459935h  mov rbp, rsp
> 0000000000459938h  sub rsp, 10h
> 000000000045993Ch  mov qword ptr [rbp-08h], rdi
> 0000000000459940h  mov rdi, rsi
> 0000000000459943h  mov rax, qword ptr [rbp-08h]
> 0000000000459947h  call qword ptr [rax]
> 000000000045994Ah  mov rsp, rbp
> 000000000045994Dh  pop rbp
> 000000000045994Eh  ret
> ;-------------------------------------
>
> ;------- SUB 0000000000459994h -------
> ; NAMED: test
> 0000000000459994h  push rbp
> 0000000000459995h  mov rbp, rsp
> 0000000000459998h  sub rsp, 10h
> 000000000045999Ch  xor esi, esi
> 000000000045999Eh  mov dword ptr [rbp-10h], esi
> 00000000004599A1h  movss xmm0, dword ptr [rbp-10h]
> 00000000004599A6h  call 0000000000459934h
> 00000000004599ABh  mov rsp, rbp
> 00000000004599AEh  pop rbp
> 00000000004599AFh  ret
> ;-------------------------------------
>
>  - testInlined() contains only the delegate call. (call qword 
> ptr [rax])
>  - test() contains a call (call 0000000000459934h) which 
> contains the
>    delegate call (call qword ptr [rax])
>
> Actually i've even had to add (pragma inline false) to show the 
> difference since DMD inlined automatically bar() in test().

Guys, you're great! Thanks a lot!


More information about the Digitalmars-d-learn mailing list