www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - virtual method pointer

reply Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I need to get a pointer to a virtual method, which is in turn a
function pointer, being set by virtual method binding.
Can anyone, please, tell me how to get it? Taking the delegate of the
method won't do, because I need it to behave exactly as a virtual
method call, except I pass the "this" explicitly.
I need this in an event handling mechanism I'm making. You derive from
the Sink class, passing your static type to the constructor, which
scans your virtual methods, that conform to specific requirements and
extracts them into an array, which later uses to dispatch the incoming
events.
It will feel much like a run-time virtual template method.

-- 
Bye,
Gor Gyolchanyan.
May 03 2012
next sibling parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <xtzgzorex gmail.com> writes:
On 03-05-2012 20:46, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate of the
 method won't do, because I need it to behave exactly as a virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You derive from
 the Sink class, passing your static type to the constructor, which
 scans your virtual methods, that conform to specific requirements and
 extracts them into an array, which later uses to dispatch the incoming
 events.
 It will feel much like a run-time virtual template method.

import std.stdio; class A { void foo() { writeln("foo called"); } } void main() { auto a = new A(); auto fn = &a.foo; auto ptr = fn.funcptr; (cast(void function(A))ptr)(a); } Prints "foo called". -- - Alex
May 03 2012
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 05/03/2012 09:01 PM, Gor Gyolchanyan wrote:
 class B: A
 {
      void foo()
      {
          writeln("B.foo called");
      }
 }

 void main()
 {
     auto a = new A();
     auto fn =&a.foo;
     auto ptr = fn.funcptr;
     auto b = new B();
     (cast(void function(A))ptr)(b);
 }

 will this work?

It should work (at least) with the D calling convention, yes. A somewhat more portable way would probably be: auto fn = &a.foo; fn.ptr = cast(void*)b; fn();
May 03 2012
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 05/03/2012 09:18 PM, Gor Gyolchanyan wrote:
 So, you're saying, that the foo function actually takes the *this,
 which we ass manually, extracts the real foo method and calls it?
 AFAIK, that shouldn't be the case. The delegate extracts the A's foo
 and call to the delegate should be a direct call to A.foo, not a
 virtual call.

I just gave an alternative to Alex' solution, but it seems both do not actually do what you are after. Is this good enough? auto fn = function(A a){return a.foo();} fn(a); fn(b);
May 03 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
class B: A
{
    void foo()
    {
        writeln("B.foo called");
    }
}

void main()
{
   auto a =3D new A();
   auto fn =3D &a.foo;
   auto ptr =3D fn.funcptr;
   auto b =3D new B();
   (cast(void function(A))ptr)(b);
}

will this work?

On Thu, May 3, 2012 at 10:54 PM, Alex R=C3=B8nne Petersen
<xtzgzorex gmail.com> wrote:
 On 03-05-2012 20:46, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate of the
 method won't do, because I need it to behave exactly as a virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You derive from
 the Sink class, passing your static type to the constructor, which
 scans your virtual methods, that conform to specific requirements and
 extracts them into an array, which later uses to dispatch the incoming
 events.
 It will feel much like a run-time virtual template method.

import std.stdio; class A { =C2=A0 =C2=A0void foo() =C2=A0 =C2=A0{ =C2=A0 =C2=A0 =C2=A0 =C2=A0writeln("foo called"); =C2=A0 =C2=A0} } void main() { =C2=A0 =C2=A0auto a =3D new A(); =C2=A0 =C2=A0auto fn =3D &a.foo; =C2=A0 =C2=A0auto ptr =3D fn.funcptr; =C2=A0 =C2=A0(cast(void function(A))ptr)(a); } Prints "foo called". -- - Alex

--=20 Bye, Gor Gyolchanyan.
May 03 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate 
 of the
 method won't do, because I need it to behave exactly as a 
 virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You 
 derive from
 the Sink class, passing your static type to the constructor, 
 which
 scans your virtual methods, that conform to specific 
 requirements and
 extracts them into an array, which later uses to dispatch the 
 incoming
 events.
 It will feel much like a run-time virtual template method.

Are you asking for the same thing I was asking for? http://forum.dlang.org/thread/jn9ops$13bs$1 digitalmars.com
May 03 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
No, because I'm not supposed to get delegates in the first place. What
I want is to have a pointer to a pointer to a function, so I can make
a true virtual call.

On Thu, May 3, 2012 at 11:09 PM, Mehrdad <wfunction hotmail.com> wrote:
 On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate of the
 method won't do, because I need it to behave exactly as a virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You derive from
 the Sink class, passing your static type to the constructor, which
 scans your virtual methods, that conform to specific requirements and
 extracts them into an array, which later uses to dispatch the incoming
 events.
 It will feel much like a run-time virtual template method.

Are you asking for the same thing I was asking for? http://forum.dlang.org/thread/jn9ops$13bs$1 digitalmars.com

-- Bye, Gor Gyolchanyan.
May 03 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
So, you're saying, that the foo function actually takes the *this,
which we ass manually, extracts the real foo method and calls it?
AFAIK, that shouldn't be the case. The delegate extracts the A's foo
and call to the delegate should be a direct call to A.foo, not a
virtual call.

On Thu, May 3, 2012 at 11:11 PM, Timon Gehr <timon.gehr gmx.ch> wrote:
 On 05/03/2012 09:01 PM, Gor Gyolchanyan wrote:
 class B: A
 {
 =C2=A0 =C2=A0 void foo()
 =C2=A0 =C2=A0 {
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 writeln("B.foo called");

 =C2=A0 =C2=A0 }
 }

 void main()
 {
 =C2=A0 =C2=A0auto a =3D new A();
 =C2=A0 =C2=A0auto fn =3D&a.foo;
 =C2=A0 =C2=A0auto ptr =3D fn.funcptr;
 =C2=A0 =C2=A0auto b =3D new B();
 =C2=A0 =C2=A0(cast(void function(A))ptr)(b);
 }

 will this work?

It should work (at least) with the D calling convention, yes. A somewhat more portable way would probably be: auto fn =3D &a.foo; fn.ptr =3D cast(void*)b; fn();

--=20 Bye, Gor Gyolchanyan.
May 03 2012
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 03 May 2012 14:46:58 -0400, Gor Gyolchanyan  
<gor.f.gyolchanyan gmail.com> wrote:

 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.

Not exactly. There is a workaround: http://www.drdobbs.com/blogs/cpp/231600610 -Steve
May 03 2012
parent deadalnix <deadalnix gmail.com> writes:
Le 03/05/2012 22:22, Gor Gyolchanyan a écrit :
 That workaround is pretty obvious, but I can't afford to make an extra
 call every time. This event system is supposed to be ultra-fast. Isn't
 there a way to get to the vtable etry itself?

Well : 1/ Such a trivial thing is surely inlined by any compiler with optimizations on. 2/ You can afford a virtual dispatch, but can't afford a function call ? I do think you are in case of premature optimizations here.
May 03 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
That workaround is pretty obvious, but I can't afford to make an extra
call every time. This event system is supposed to be ultra-fast. Isn't
there a way to get to the vtable etry itself?

On Thu, May 3, 2012 at 11:26 PM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 On Thu, 03 May 2012 14:46:58 -0400, Gor Gyolchanyan
 <gor.f.gyolchanyan gmail.com> wrote:

 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.

Not exactly. =C2=A0There is a workaround: http://www.drdobbs.com/blogs/cpp/231600610 -Steve

--=20 Bye, Gor Gyolchanyan.
May 03 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
No, I intend to call these methods very frequently and I can't afford
any performance loss.

What I expect to have is something like a virtual table entry. I tied
looking at the virtual tables and searching for the method delegate's
.funcptr in the vtable, but didn't find it.

Having that vtable entry would allow me to

On Thu, May 3, 2012 at 11:23 PM, Timon Gehr <timon.gehr gmx.ch> wrote:
 On 05/03/2012 09:18 PM, Gor Gyolchanyan wrote:
 So, you're saying, that the foo function actually takes the *this,
 which we ass manually, extracts the real foo method and calls it?
 AFAIK, that shouldn't be the case. The delegate extracts the A's foo
 and call to the delegate should be a direct call to A.foo, not a
 virtual call.

I just gave an alternative to Alex' solution, but it seems both do not actually do what you are after. Is this good enough? auto fn = function(A a){return a.foo();} fn(a); fn(b);

-- Bye, Gor Gyolchanyan.
May 03 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Good point. That raises the question: How should I make the fastest
possible dynamic dispatch of this kind?

On Fri, May 4, 2012 at 12:57 AM, deadalnix <deadalnix gmail.com> wrote:
 Le 03/05/2012 22:22, Gor Gyolchanyan a =C3=A9crit :

 That workaround is pretty obvious, but I can't afford to make an extra
 call every time. This event system is supposed to be ultra-fast. Isn't
 there a way to get to the vtable etry itself?

Well : 1/ Such a trivial thing is surely inlined by any compiler with optimizati=

 on.
 2/ You can afford a virtual dispatch, but can't afford a function call ?

 I do think you are in case of premature optimizations here.

--=20 Bye, Gor Gyolchanyan.
May 03 2012
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 03 May 2012 16:22:56 -0400, Gor Gyolchanyan  
<gor.f.gyolchanyan gmail.com> wrote:

 That workaround is pretty obvious, but I can't afford to make an extra
 call every time. This event system is supposed to be ultra-fast. Isn't
 there a way to get to the vtable etry itself?

Well, you can use the ABI to determine the __vtbl location, but I'm not sure if there's a way in compile-time to get the __vtbl index to use. I suppose you could iterate over the vtbl entries and find the function that matches. It would be O(n) to construct the call function, but O(1) to call it. -Steve
May 03 2012
prev sibling next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Thu, 03 May 2012 16:57:01 -0400, deadalnix <deadalnix gmail.com> wrot=
e:

 Le 03/05/2012 22:22, Gor Gyolchanyan a =C3=A9crit :
 That workaround is pretty obvious, but I can't afford to make an extr=


 call every time. This event system is supposed to be ultra-fast. Isn'=


 there a way to get to the vtable etry itself?

1/ Such a trivial thing is surely inlined by any compiler with =

 optimizations on.

I wouldn't count on it. You can't inline a lambda that is used somewher= e = it's not declared.
 2/ You can afford a virtual dispatch, but can't afford a function call=

In my experience, virtual calls are nearly as cheap as normal calls. Bu= t = doubling the function call setup/teardown/call is not insignificant. It= = all depends on how much time is spent in the function being called.
 I do think you are in case of premature optimizations here.

This is very likely, it all depends on what you are doing *inside* the = call. -Steve
May 03 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Basically I want to make a dynamic typing subsystem in D. I want to
take a given object of a static type, construct a dynamic interface
for it and later use that interface to event handling.
To construct a dynamic interface, I need to extract the virtual
methods without resolving them prematurely.

On Fri, May 4, 2012 at 1:11 AM, Steven Schveighoffer
<schveiguy yahoo.com> wrote:
 On Thu, 03 May 2012 16:57:01 -0400, deadalnix <deadalnix gmail.com> wrote=

 Le 03/05/2012 22:22, Gor Gyolchanyan a =C3=A9crit :
 That workaround is pretty obvious, but I can't afford to make an extra
 call every time. This event system is supposed to be ultra-fast. Isn't
 there a way to get to the vtable etry itself?

1/ Such a trivial thing is surely inlined by any compiler with optimizations on.

I wouldn't count on it. =C2=A0You can't inline a lambda that is used some=

 it's not declared.


 2/ You can afford a virtual dispatch, but can't afford a function call ?

In my experience, virtual calls are nearly as cheap as normal calls. =C2=

 doubling the function call setup/teardown/call is not insignificant. =C2=

 depends on how much time is spent in the function being called.


 I do think you are in case of premature optimizations here.

This is very likely, it all depends on what you are doing *inside* the ca=

 -Steve

--=20 Bye, Gor Gyolchanyan.
May 03 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
class Foo
{
	void test() { }
}

void main(string[] args)
{
	auto f = new Foo();
	stderr.writeln(f.__vptr[6]);
	auto del = (&f.test);
	stderr.writeln(del.funcptr);
}
May 03 2012
prev sibling next sibling parent Jacob Carlborg <doob me.com> writes:
On 2012-05-03 20:46, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate of the
 method won't do, because I need it to behave exactly as a virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You derive from
 the Sink class, passing your static type to the constructor, which
 scans your virtual methods, that conform to specific requirements and
 extracts them into an array, which later uses to dispatch the incoming
 events.
 It will feel much like a run-time virtual template method.

You can use two delegates: class Foo { void bar () { writeln("Foo"); } void delegate () resolveVirtualCall () { return &bar; } static void forwardVirtualCall (Foo object) { void delegate () delegate () dg; dg.ptr = cast(void*) object; dg.funcptr = &resolveVirtualCall; dg()(); } } class Bar : Foo { void bar () { writeln("Bar"); } } void main() { Foo b = new Bar; Foo.forwardVirtualCall(b); } Will print "Bar". If "resolveVirtualCall" is not used and "bar" is used directly instead, it will print "Foo". -- /Jacob Carlborg
May 04 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Thanks, I'll look into it.

On Fri, May 4, 2012 at 11:25 AM, Jacob Carlborg <doob me.com> wrote:
 On 2012-05-03 20:46, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate of the
 method won't do, because I need it to behave exactly as a virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You derive from
 the Sink class, passing your static type to the constructor, which
 scans your virtual methods, that conform to specific requirements and
 extracts them into an array, which later uses to dispatch the incoming
 events.
 It will feel much like a run-time virtual template method.

You can use two delegates: class Foo { =C2=A0 =C2=A0void bar () =C2=A0 =C2=A0{ =C2=A0 =C2=A0 =C2=A0 =C2=A0writeln("Foo"); =C2=A0 =C2=A0} =C2=A0 =C2=A0void delegate () resolveVirtualCall () =C2=A0 =C2=A0{ =C2=A0 =C2=A0 =C2=A0 =C2=A0return &bar; =C2=A0 =C2=A0} =C2=A0 =C2=A0static void forwardVirtualCall (Foo object) =C2=A0 =C2=A0{ =C2=A0 =C2=A0 =C2=A0 =C2=A0void delegate () delegate () dg; =C2=A0 =C2=A0 =C2=A0 =C2=A0dg.ptr =3D cast(void*) object; =C2=A0 =C2=A0 =C2=A0 =C2=A0dg.funcptr =3D &resolveVirtualCall; =C2=A0 =C2=A0 =C2=A0 =C2=A0dg()(); =C2=A0 =C2=A0} } class Bar : Foo { =C2=A0 =C2=A0void bar () =C2=A0 =C2=A0{ =C2=A0 =C2=A0 =C2=A0 =C2=A0writeln("Bar"); =C2=A0 =C2=A0} } void main() { =C2=A0 =C2=A0Foo b =3D new Bar; =C2=A0 =C2=A0Foo.forwardVirtualCall(b); } Will print "Bar". If "resolveVirtualCall" is not used and "bar" is used directly instead, it will print "Foo". -- /Jacob Carlborg

--=20 Bye, Gor Gyolchanyan.
May 04 2012
prev sibling next sibling parent "jerro" <a a.com> writes:
On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate 
 of the
 method won't do, because I need it to behave exactly as a 
 virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You 
 derive from
 the Sink class, passing your static type to the constructor, 
 which
 scans your virtual methods, that conform to specific 
 requirements and
 extracts them into an array, which later uses to dispatch the 
 incoming
 events.
 It will feel much like a run-time virtual template method.

If I understand you correctly, you could use something like this template methodToFunction(A, string method) { auto methodToFunction(A a, ParameterTypeTuple!(mixin("A." ~ method)) p) { return mixin("&a." ~ method)(p); } } If you use it like this: class Foo { int m; void bar(int a, string b) { writefln("%s\t%s\t%s", m, a, b); } } class Child: Foo { void bar(int a, string b) { writeln("child"); } } void main() { auto foo = new Foo; foo.m = 1; auto child = new Child; auto bar = &methodToFunction!(Foo, "bar"); bar(foo, 2, "3"); bar(child, 2, "3"); } It prints: 1 2 3 child It won't work correctly with overloading, though. It could be made to work with overloading, but then you would have to pass all the parameter types of the method to the methodToFunction template.
May 04 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Does this have an overhead over calling virtual method directly?

On Fri, May 4, 2012 at 1:30 PM, jerro <a a.com> wrote:
 On Thursday, 3 May 2012 at 18:47:11 UTC, Gor Gyolchanyan wrote:
 I need to get a pointer to a virtual method, which is in turn a
 function pointer, being set by virtual method binding.
 Can anyone, please, tell me how to get it? Taking the delegate of the
 method won't do, because I need it to behave exactly as a virtual
 method call, except I pass the "this" explicitly.
 I need this in an event handling mechanism I'm making. You derive from
 the Sink class, passing your static type to the constructor, which
 scans your virtual methods, that conform to specific requirements and
 extracts them into an array, which later uses to dispatch the incoming
 events.
 It will feel much like a run-time virtual template method.

If I understand you correctly, you could use something like this template methodToFunction(A, string method) { =C2=A0 =C2=A0 auto methodToFunction(A a, ParameterTypeTuple!(mixin("A." ~=

 =C2=A0 =C2=A0 {
 =C2=A0 =C2=A0 =C2=A0 =C2=A0 return mixin("&a." ~ method)(p);
 =C2=A0 =C2=A0 }
 }

 If you use it like this:

 class Foo
 {
 =C2=A0 =C2=A0int m;

 =C2=A0 =C2=A0void bar(int a, string b)
 =C2=A0 =C2=A0{
 =C2=A0 =C2=A0 =C2=A0 =C2=A0writefln("%s\t%s\t%s", m, a, b);
 =C2=A0 =C2=A0}
 }

 class Child: Foo
 {
 =C2=A0 =C2=A0void bar(int a, string b)
 =C2=A0 =C2=A0{
 =C2=A0 =C2=A0 =C2=A0 =C2=A0writeln("child");
 =C2=A0 =C2=A0}
 }

 void main()
 {
 =C2=A0 =C2=A0auto foo =3D new Foo;
 =C2=A0 =C2=A0foo.m =3D 1;
 =C2=A0 =C2=A0auto child =3D new Child;
 =C2=A0 =C2=A0auto bar =3D &methodToFunction!(Foo, "bar");
 =C2=A0 =C2=A0bar(foo, 2, "3");
 =C2=A0 =C2=A0bar(child, 2, "3");
 }

 It prints:

 1 =C2=A0 =C2=A0 =C2=A0 2 =C2=A0 =C2=A0 =C2=A0 3
 child

 It won't work correctly with overloading, though. It could
 be made to work with overloading, but then you would
 have to pass all the parameter types of the method to
 the methodToFunction template.

--=20 Bye, Gor Gyolchanyan.
May 04 2012
prev sibling next sibling parent "jerro" <a a.com> writes:
On Friday, 4 May 2012 at 09:51:51 UTC, Gor Gyolchanyan wrote:
 Does this have an overhead over calling virtual method directly?

If you call the function directly, it probably gets inlined. If you call it through a function pointer, it does have some overhead over calling the virtual method directly.
May 04 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
So, the only overhead in making a virtual call this way over calling
the method directly is exactly 1 extra function call?

On Fri, May 4, 2012 at 2:02 PM, jerro <a a.com> wrote:
 On Friday, 4 May 2012 at 09:51:51 UTC, Gor Gyolchanyan wrote:
 Does this have an overhead over calling virtual method directly?

If you call the function directly, it probably gets inlined. If you call it through a function pointer, it does have some overhead over calling the virtual method directly.

-- Bye, Gor Gyolchanyan.
May 04 2012
prev sibling next sibling parent "jerro" <a a.com> writes:
On Friday, 4 May 2012 at 10:05:54 UTC, Gor Gyolchanyan wrote:
 So, the only overhead in making a virtual call this way over 
 calling
 the method directly is exactly 1 extra function call?

I guess so. You are calling a function that does a virtual call and doesn't do anything else, so what other overhead could there be?
May 04 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Great! Thanks!
After I'm done with this, I'll propose adding it to Phobos.
A genuine dynamic dispatch mechanism would be very useful.

On Fri, May 4, 2012 at 6:04 PM, jerro <a a.com> wrote:
 On Friday, 4 May 2012 at 10:05:54 UTC, Gor Gyolchanyan wrote:
 So, the only overhead in making a virtual call this way over calling
 the method directly is exactly 1 extra function call?

I guess so. You are calling a function that does a virtual call and doesn't do anything else, so what other overhead could there be?

-- Bye, Gor Gyolchanyan.
May 04 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
Did you see my solution? I think it's what you're looking for...

On Friday, 4 May 2012 at 10:05:54 UTC, Gor Gyolchanyan wrote:
 So, the only overhead in making a virtual call this way over 
 calling
 the method directly is exactly 1 extra function call?

 On Fri, May 4, 2012 at 2:02 PM, jerro <a a.com> wrote:
 On Friday, 4 May 2012 at 09:51:51 UTC, Gor Gyolchanyan wrote:
 Does this have an overhead over calling virtual method 
 directly?

If you call the function directly, it probably gets inlined. If you call it through a function pointer, it does have some overhead over calling the virtual method directly.


May 04 2012
prev sibling next sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
Yes! Your solution looks exactly like what I wanted. The reason why I
considered additional alternatives is because your solutions looks
very fast (YES!!!), but not very portable and safe, so after testing,
if it turns out to be inconsistent, I'll have to use something else.

On Fri, May 4, 2012 at 6:12 PM, Mehrdad <wfunction hotmail.com> wrote:
 Did you see my solution? I think it's what you're looking for...


 On Friday, 4 May 2012 at 10:05:54 UTC, Gor Gyolchanyan wrote:
 So, the only overhead in making a virtual call this way over calling
 the method directly is exactly 1 extra function call?

 On Fri, May 4, 2012 at 2:02 PM, jerro <a a.com> wrote:
 On Friday, 4 May 2012 at 09:51:51 UTC, Gor Gyolchanyan wrote:
 Does this have an overhead over calling virtual method directly?

If you call the function directly, it probably gets inlined. If you call it through a function pointer, it does have some overhead over calling the virtual method directly.



-- Bye, Gor Gyolchanyan.
May 04 2012
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
Ah okay. Yeah it's not 'safe' at all... but I think the '6' comes 
from the number of members that Foo has. If you figure out how 
many methods there are in the v-table, then that should get you 
the index (though don't quote me on this... you should look at 
the compiler source code if you want to be really sure).

On Friday, 4 May 2012 at 14:16:49 UTC, Gor Gyolchanyan wrote:
 Yes! Your solution looks exactly like what I wanted. The reason 
 why I
 considered additional alternatives is because your solutions 
 looks
 very fast (YES!!!), but not very portable and safe, so after 
 testing,
 if it turns out to be inconsistent, I'll have to use something 
 else.

May 04 2012
prev sibling parent Gor Gyolchanyan <gor.f.gyolchanyan gmail.com> writes:
I'm not going to cherry-pick methods in any case. I'll have all
methods analyzed at compile time, by iterating over the overloads of
each method and have them searched in the vtable to get their indices
at launch time. It'll be easy to construct a dynamic virtual method
call.

On Fri, May 4, 2012 at 6:26 PM, Mehrdad <wfunction hotmail.com> wrote:
 Ah okay. Yeah it's not 'safe' at all... but I think the '6' comes from the
 number of members that Foo has. If you figure out how many methods there are
 in the v-table, then that should get you the index (though don't quote me on
 this... you should look at the compiler source code if you want to be really
 sure).


 On Friday, 4 May 2012 at 14:16:49 UTC, Gor Gyolchanyan wrote:
 Yes! Your solution looks exactly like what I wanted. The reason why I
 considered additional alternatives is because your solutions looks
 very fast (YES!!!), but not very portable and safe, so after testing,
 if it turns out to be inconsistent, I'll have to use something else.


-- Bye, Gor Gyolchanyan.
May 04 2012