Determining whether a derived class overrides a virtual memberfunction

L

lord trousers

I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The
default implementation does nothing and always will, and most types
won't need to override it. It'd be nice if I could test for overrides,
but the obvious thing doesn't work:

class Object {
// ...
virtual void finalize() { }
// ...
};

// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();

// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'

I'd love to do what it says, but I don't know the type of the object.
Is there an easy way to make this same test? I want to avoid RTTI and
compiler-specific language extensions, and also make it externally
transparent.

Neil
 
M

Mark

I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The
default implementation does nothing and always will, and most types
won't need to override it. It'd be nice if I could test for overrides,
but the obvious thing doesn't work:

class Object {
// ...
virtual void finalize() { }
// ...
};

// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();

// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'

I'd love to do what it says, but I don't know the type of the object.
Is there an easy way to make this same test? I want to avoid RTTI and
compiler-specific language extensions, and also make it externally
transparent.

Neil

Funny you should post this a few minutes before me... I'm waiting for
the same answer. Let me know if you figure anything out?
 
E

Erik Wikström

I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The
default implementation does nothing and always will, and most types
won't need to override it. It'd be nice if I could test for overrides,
but the obvious thing doesn't work:

class Object {
// ...
virtual void finalize() { }
// ...
};

// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();

// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'

I'd love to do what it says, but I don't know the type of the object.
Is there an easy way to make this same test? I want to avoid RTTI and
compiler-specific language extensions, and also make it externally
transparent.

I do not think you can do that in C++, and there are two reasons for
that: the language was designed to hide this kind of stuff from the
users, and this sounds like implementation specific territory, so even
if you find something that seems to work it might not on another
compiler or in the next version of your current.

You could use templates to get to know the actual type of the object,
but that seems undesirable, and I am not sure even that would work. If
you could get the member function pointer for the object it might quite
possibly be just an index into the vtable, and what you really want to
compare is the values in the entries those indexes points to. But since
there is no way in C++ to access the vtables you can not do that either.

#include <iostream>

struct B
{
virtual void bar() { }
};

struct D1 : public B
{
virtual void bar() { }
};

struct D2 : public B
{
virtual void bar() { }
};

int main()
{
std::cout << &D1::bar << std::endl;
std::cout << &D2::bar << std::endl;
}
 
P

Paavo Helde

I'm implementing a garbage collector in C++ for a fun new language
(don't ask), and I've found that it spends a good amount of time
calling "finalize" on objects that are freed/not relocated. The

Are you sure the "if" check would cost less than a virtual function call?

If yes, then AFAIK what you want can't be done in the language. I guess
you could do it non-portably for each platform/implementation you
support. If you only have single inheritance, then it should be
relatively simple: the vtable pointer is usually in the beginning of
Object, with the knowledge about which vtable slot holds your finalize()
function pointer you could be able to compare the pointers.

However, I strongly suggest to avoid this path; if the virtual finalize()
call is costing too much there is probably something wrong in the overall
design, maybe an attempt to handle each integer in an array as a separate
object?

HTH
Paavo
 
J

James Kanze

I'm implementing a garbage collector in C++ for a fun new
language (don't ask), and I've found that it spends a good
amount of time calling "finalize" on objects that are
freed/not relocated. The default implementation does nothing
and always will, and most types won't need to override it.
It'd be nice if I could test for overrides, but the obvious
thing doesn't work:
class Object {
// ...
virtual void finalize() { }
// ...
};
// In the collector:
Object* pobj = <stuff>;
if (&pobj->finalize != &hv_Object::finalize)
pobj->finalize();
// GCC:
// ISO C++ forbids taking the address of a bound member function
// to form a pointer to member function. Say
'&hv_Object::finalize'
I'd love to do what it says, but I don't know the type of the
object. Is there an easy way to make this same test? I want
to avoid RTTI and compiler-specific language extensions, and
also make it externally transparent.

Let me see if I understand: you want to avoid RTTI, but you want
RTTI. As soon as you try to find out something about the
dynamic type, you need RTTI---generally, a virtual function is
cheaper (in runtime) than anything else.

With regards to finalization with garbage collection, the usual
solution is to require explicit registration. This means a
(very slight) amount of extra work for the programmer who needs
finalization (but such cases are rare), but also means that you
don't have to derive everything from a common base---garbage
collection can also work for arrays of char, or what have you.
(This is how the Boehm collector works, but of course, it also
works with C, where virtual functions aren't an option.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top