Boost logo

Boost :

Subject: Re: [boost] [bind] virtual function pointer woes
From: Chris (indy271828_at_[hidden])
Date: 2009-01-09 13:40:31


Did c++0x address this? Here is a macro example, call
MAKE_CALL_BASE(thefunc); for every virtual function.

#include <iostream>

struct B { virtual void f() { std::printf("B\n"); } };
struct D : B { virtual void f() { std::printf("D\n"); } };

#define MAKE_CALL_BASE(X) \
template <typename BASE, typename... Args> \
void call_##X(BASE *d, Args... args) \
{ \
    d->BASE::X (args...); \
}

MAKE_CALL_BASE(f);

int main()
{
    B * b = new D;
    call_f<B>(b);
}

Eric Niebler wrote:
> Jon Biggar wrote:
>> Chris wrote:
>>> As far as I know, no. If you find a true answer, please tell me.
>>> I had this problem once as well and looked into it on the
>>> newsgroups. There may be a non-portable way with the vtable and
>>> asm, but not generally. My suggestion would be to make a split
>>>
>>> _nv function that does the call
>>>
>>> struct B { virtual void f() { f_nv(); } void f_nv()
>>> {std::printf("B\n"); } };
>>> struct D : B { virtual void f() { f_nv(); } void f_nv() {
>>> std::printf("D\n"); } };
>>>
>>> And then bind _nv;
>
> This is not an option as the classes B and D are not modifiable.
>
>> Or just create a new free function:
>>
>> inline void call_B_f(D &d)
>> {
>> d->B::f();
>> }
>>
>> boost::bind(&call_B_f, _1)(d);
>
> Sure, but I'm going to need a lot of these little functions and it's a
> pain. C++ has what I want (the member function I want to bind to), but
> it's not giving it to me. Very frustrating.
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk