Re: Why pointer to member function?
From: Ben (ben_yu_at_asc.aon.com)
Date: 06/11/04
- Next message: Kai-Uwe Bux: "Re: tricky stringstream-based temporary"
- Previous message: Siemel Naran: "Re: switch vs. if"
- In reply to: Jeff Flinn: "Re: Why pointer to member function?"
- Next in thread: Andrey Tarasevich: "Re: Why pointer to member function?"
- Reply: Andrey Tarasevich: "Re: Why pointer to member function?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 10 Jun 2004 21:42:17 -0700
Jeff,
Thanks for refering me to this nice article.
boost function does look clean and powerful. I have no doubt about the
usefulness of this lib.
However, it does not answer my question.
Let me rephrase my question and reasoning:
I want a simple,clean,cheap way that can save the member function
closure into a vector, which implies that the element size has to be
fixed for any type of closure.
And because of the following 2 reasons, it is not possible to save a
ptm INLINE in a vector:
1. pointers to member function don't have the same size and the
maximum size cannot be even predictable.
2. don't want to save a pointer to a pointer to a member function
because a pointer to a member function is normally a constant.
the boost lib does prove that I'm not wrong on this. It uses "new" to
allocate space for the closure in heap! That gets around the problem.
Although the interface of boost looks very pleasant and all the memory
management are hidden under the hood, the fact that "new" has to be
used for what should have been a vanilla thing makes me feel sad.
In my specific case, I don't need all the rich features such as
bindXXX, all I need is to pass a member function for a hook callback.
And calling a "new" for every closure where I don't have to is a bit
of a overkill to me. I don't quite like to pay that price.
My final question about "why pointer to member function at all?" came
from this thought:
What if, let's just say "what if", there's no "PTM" at all, MyClass::f
simply yields a function pointer of type "int (*)(MyClass*)"?
Isn't that simpler, faster and has better compatibility with the
already-existing function pointer? And, more importantly, does it
work?
The "D&E" book justifies PTM as:
"... This only worked with a liberal sprinkling of explicit casts that
never ought to have worked in the first place. It also relied on the
assumption that a member function is passed its object pointer as the
first argument in the way Cfront implements it"
My arguments are:
1. It can be type safe to make T::*f a function pointer of
"int(*)(T*)", if the language wants to do so. Since I can create the
wrapper function manually, there's no reason the compiler cannot
implicitly create it.
2. Making "MyClass::f" a pointer of type "int(*)(T*)" does NOT rely on
an implementation where the "this pointer" is passed as the first
argument.
Evidence? Well, look at the wrapper function again.
Whatever the implementation is, you can pass it as the first param,
the second, or weirdly enough - from a global variable, the function
pointer "MyClass::f" simply points to a function from which you can
eventually invoke the real underlying member function.
There's no promise made that this function must have the same address
of the real "member function". It may be the same address of the real
function, may be an address of a proxy function, all up to the
implementation.
3. Even if we do want a PTM where we can enjoy the favorable syntax
"p->ptm(...)", the implementation of PTM can still be such function
pointer.
- Next message: Kai-Uwe Bux: "Re: tricky stringstream-based temporary"
- Previous message: Siemel Naran: "Re: switch vs. if"
- In reply to: Jeff Flinn: "Re: Why pointer to member function?"
- Next in thread: Andrey Tarasevich: "Re: Why pointer to member function?"
- Reply: Andrey Tarasevich: "Re: Why pointer to member function?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|