Topic: Generic member pointer


Author: "Marco Manfredini" <marquise2000@gmx.net>
Date: 2000/09/25
Raw View


"Edward Diener" <eddielee@abraxis.com> wrote in message
news:39CABAB0.8A696CFA@abraxis.com...
> Balog Pal wrote:
> >
[snip]

> This concept of "generic member function pointer" has been discussed
previously
> in this newgroup since the Borland C++ Builder compiler under Windows
> implements, as an extension to C++, essentially a "generic member function
> pointer" using the keyword "__closure", which may be a confusing name
since
> closure refers to different concepts in other languages. Essentially
Borland's
> "generic member function pointer" holds the address of both an object
instance
> as well as a member function within that instance, and can be assigned to
using
> any member function in any object which matches the signature of the
> "__closure". Stroustrup discusses the possibility of such a type in a note
to
> 5.5 of the ARM using the term "bound pointer" but it has never become part
of
> the C++ language.
>

The question for bound-member-pointers / closures /
member-function-callbacks seems to pop up more often in the last time. Ways
to emulate them in C++ have been posted a couple of times and I think it
should go to the FAQ by now. It begins to become a popular practice by now.
Balog, the answer is deja out there, and I also remeber a couple of pages
that describe such techniques! Goggle! I must go now.

--
-- Marco

I saw the LEGO VERSION of STAR WARS!

dig @138.195.138.195 goret.org. axfr | grep '^c..\..*A' | sort | cut -b5-36
| perl -e 'while(<>){print pack("H32",$_)}' | gzip -d




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: Fri, 22 Sep 2000 06:18:15 GMT
Raw View
Edward Diener wrote:
>
> Balog Pal wrote:
>
> > I recently had a situation where I neded to pass a pointer to member
> > function, form
> > void (foo::* pFunc)() . To make it more interesting, it was passed as a
> > generic thing, the recipient had knowledge about what class is actually
> > involved, obtained an instance, made te appropriate cast, then and had to
> > make the call.
> >
> > Not finding anything appropriate to hold the ?::* pointer, I tried to put
> > into the at least resembling
> > void (*pFunc)()
> > To my surprise, the cast didn't work for that, no matter which, reinterpret,
> > or the old-style.  I thought a pointer to member is just another kind of a
> > pointer-to-function, so it should be compatible.

A pointer to member function is very different from an ordinary function
pointer. It is often implemented as no more than an index into the
vtable. A pointer to data member of MyClass can be implemented as a
simple offset, storable in any integer type big enough to store
sizeof(MyClass), which might be much smaller than a typical data
pointer.

> > Then I tried to put it into a void *, that looked worse. But that also
> > failed to compile.
> > So here the programmer is forced to use a memcpy that is even less safer,
> > but also hides in the code?

No. See below.

> You can only put the pointer to member function into a variable of the exact
> same type. In C++ there is no such thing as a "generic member function pointer".
> Casting to other types won't work and memcpy is a horrid hack.

Incorrect. Section 5.2.10p9 says that using reinterpret_cast<>:

'An rvalue of type "pointer to member of X of type T1" can be explicitly
converted to an rvalue of type "pointer to member of Y of type T2" if T1
and T2 ar both function types or both object types. 66) The null member
pointer value (4.11) is converted to the null member pointer value of
the destination type. The result of this conversion is unspecified,
except in the following cases:'

'-- converting an rvalue of type "pointer to member function" to a
different pointer to member function type and back to its original type
yields the original pointer to member value.'

'-- converting an rvalue of type "pointer to data member of X of type
T1" to the type "pointer to data member Y of type T2" (where the
alignment requirements of T2 are no stricter than those of T1) and back
to its original type yields the original pointer to member value.'

Thus, while you can't convert a pointer to member function to 'void *',
you can convert it to any other pointer to member function type. Choose
one, or define one for your specific needs. Just make sure you always
convert it back to the right type before de-referencing it.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Balog Pal" <pasa@lib.hu>
Date: 2000/09/21
Raw View
I recently had a situation where I neded to pass a pointer to member
function, form
void (foo::* pFunc)() . To make it more interesting, it was passed as a
generic thing, the recipient had knowledge about what class is actually
involved, obtained an instance, made te appropriate cast, then and had to
make the call.

Not finding anything appropriate to hold the ?::* pointer, I tried to put
into the at least resembling
void (*pFunc)()
To my surprise, the cast didn't work for that, no matter which, reinterpret,
or the old-style.  I thought a pointer to member is just another kind of a
pointer-to-function, so it should be compatible.
Then I tried to put it into a void *, that looked worse. But that also
failed to compile.
So here the programmer is forced to use a memcpy that is even less safer,
but also hides in the code?

(I finally found it easier to pu the pointer in the class instance, where
it's already the correct type, but that may not be the possible solution
everywhere. And the situation is pretty common, namy system API's require a
certain format of the function as callback, like the one starting a thread.)

Paul



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Edward Diener <eddielee@abraxis.com>
Date: Fri, 22 Sep 2000 02:27:41 GMT
Raw View
Balog Pal wrote:

> I recently had a situation where I neded to pass a pointer to member
> function, form
> void (foo::* pFunc)() . To make it more interesting, it was passed as a
> generic thing, the recipient had knowledge about what class is actually
> involved, obtained an instance, made te appropriate cast, then and had to
> make the call.
>
> Not finding anything appropriate to hold the ?::* pointer, I tried to put
> into the at least resembling
> void (*pFunc)()
> To my surprise, the cast didn't work for that, no matter which, reinterpret,
> or the old-style.  I thought a pointer to member is just another kind of a
> pointer-to-function, so it should be compatible.
> Then I tried to put it into a void *, that looked worse. But that also
> failed to compile.
> So here the programmer is forced to use a memcpy that is even less safer,
> but also hides in the code?

You can only put the pointer to member function into a variable of the exact
same type. In C++ there is no such thing as a "generic member function pointer".
Casting to other types won't work and memcpy is a horrid hack.

This concept of "generic member function pointer" has been discussed previously
in this newgroup since the Borland C++ Builder compiler under Windows
implements, as an extension to C++, essentially a "generic member function
pointer" using the keyword "__closure", which may be a confusing name since
closure refers to different concepts in other languages. Essentially Borland's
"generic member function pointer" holds the address of both an object instance
as well as a member function within that instance, and can be assigned to using
any member function in any object which matches the signature of the
"__closure". Stroustrup discusses the possibility of such a type in a note to
5.5 of the ARM using the term "bound pointer" but it has never become part of
the C++ language.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]