Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Address of pure virtual function -- is it legal?

304 views
Skip to first unread message

luigi.b...@gmail.com

unread,
Feb 21, 2008, 11:31:16 PM2/21/08
to

Hi all,
I'm wondering if it's possible to take the address of a pure
virtual function, as in the code below. The program seems to work
with a few compilers, namely, the call through the pointer dispatches
to the derived-class implementation; but it could be an implementation
artifact for all I know. I had a look at the standard, but those that
seemed the relevant sections (5.3.1, 8.3.3, and 10.3, and 10.4) don't
mention this particular case. I'd be grateful if you could tell me
whether or not the code below is valid (and possibly where the
standard says so.)

Thanks,
Luigi

-------------

#include <iostream>
#include <functional>

class Foo {
public:
virtual ~Foo() {}
virtual double bar(double x) const = 0;
double test(double x) const {
double (Foo::*f)(double) const = &Foo::bar;
return std::bind1st(std::mem_fun(f),this)(x);
}
};

class Bar : public Foo {
public:
double bar(double x) const { return x/2.0; }
};

class Baz : public Foo {
public:
double bar(double x) const { return x*2.0; }
};

int main() {
std::cout << Bar().test(42.0) << std::endl;
std::cout << Baz().test(42.0) << std::endl;
return 0;
}


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Amal

unread,
Feb 22, 2008, 9:29:38 AM2/22/08
to

{ Edits: quoted clc++m banner removed. Please don't quote extraneous
material. -mod }

Hi,

Getting the address of a pure virtual function is valid in all
cases. Whenever the address of a virtual/pure virtual function is
taken the vtable is referred and correct address is retrieved. Hence
it is always valid what you have done and it must be compatible across
any c++ compilers.

Thanks,
Amal P

luigi.b...@gmail.com

unread,
Feb 25, 2008, 6:36:09 AM2/25/08
to
On Feb 22, 3:29 pm, Amal <amal.paramb...@gmail.com> wrote:
> Getting the address of a pure virtual function is valid in all
> cases. Whenever the address of a virtual/pure virtual function is
> taken the vtable is referred and correct address is retrieved. Hence
> it is always valid what you have done and it must be compatible across
> any c++ compilers.

Thank you. May anybody point me to where this is stated in the
standard?
(Not that I distrust you, of course. It's just to further learn my way
about it.)

Luigi

Greg Herlihy

unread,
Feb 26, 2008, 1:44:39 PM2/26/08
to
On Feb 25, 3:36 am, luigi.balla...@gmail.com wrote:
> On Feb 22, 3:29 pm, Amal <amal.paramb...@gmail.com> wrote:
>
> > Getting the address of a pure virtual function is valid in all
> > cases. Whenever the address of a virtual/pure virtual function is
> > taken the vtable is referred and correct address is retrieved. Hence
> > it is always valid what you have done and it must be compatible across
> > any c++ compilers.
>
> Thank you. May anybody point me to where this is stated in the
> standard?

I doubt that anyone will be able to provide such a citation - because
nowhere in the C++ Standard is there any language that states that a
member pointer to a pure virtual method is OK. But - and more
importantly - nowhere in the C++ Standard is there any language that
states that a member pointer to a pure virtual method is not OK.

The C++ Standard would have to prohibit member pointers to pure
virtual methods explicitly (just as the Standard does, for example,
with member pointers to static member functions) in order to make such
member pointers illegal. Otherwise, in the absence of any language to
that effect, member pointers to pure virtual methods must be legal -
and must behave no differently than any other kind of member pointer.

Greg

Amal

unread,
Feb 26, 2008, 1:44:13 PM2/26/08
to
On Feb 25, 4:36 pm, luigi.balla...@gmail.com wrote:
> On Feb 22, 3:29 pm, Amal <amal.paramb...@gmail.com> wrote:
>
> > Getting the address of a pure virtual function is valid in all
> > cases. Whenever the address of a virtual/pure virtual function is
> > taken the vtable is referred and correct address is retrieved. Hence
> > it is always valid what you have done and it must be compatible across
> > any c++ compilers.
>
> Thank you. May anybody point me to where this is stated in the
> standard?
> (Not that I distrust you, of course. It's just to further learn my way
> about it.)

{ Edits: quoted signature and clc++m banner removed. Please don't quote
extraneous material. -mod }


Hi,

Section 8.3.3, sub index 3 represents the invalid cases of a pointer
to member. All other cases except specially mentioned are valid use.
So go ahead and use it. It is safe.

Amal

0 new messages