views:

214

answers:

3

Hello,

I am currently implementing a timer/callback system using Don Clugston's fastdelegates. (see http://www.codeproject.com/KB/cpp/FastDelegate.aspx)

Here is the starting code:

struct TimerContext
{
};

void free_func( TimerContext* )
{
}

struct Foo
{
    void member_func( TimerContext* )
    {
    }
};

Foo f;
MulticastDelegate< void (TimerContext*) > delegate;

delegate += free_func;
delegate += bind( &Foo::member_func, &f );

Okay, but now, i wish the user to be able to subclass TimerContext to store and send his own structures to the callbacks. The purpose here is to prevent the user from having to downcast the TimerContext himself

struct TimerContext
{
};

struct MyTimerContext : TimerContext
{
    int user_value;
};

void free_func( TimerContext* )
{
}

void free_func2( MyTimerContext* )
{
}

struct Foo
{
    void member_func( TimerContext* )
    {
    }

    void member_func2( MyTimerContext* )
    {
    }
};

Foo f;
MulticastDelegate< void (TimerContext*) > delegate;

delegate += free_func;
delegate += free_func2;
delegate += bind( &Foo::member_func,  &f );
delegate += bind( &Foo::member_func2, &f );

As you guessed, GCC won't let me do that :)

error: invalid conversion from `void (*)(MyTimerContext*)' to `void (*)(TimerContext*)'
error:   initializing argument 1 of `delegate::Delegate<R ()(Param1)>::Delegate(R (*)(Param1)) [with R = void, Param1 = TimerContext*]'

So now my question is: If I force the cast using reinterpret_cast, it'll work, but will it be safe ?

PS: These are time-critical callbacks, heavy virtual-oriented solutions are considered impracticable :/

+5  A: 

C++ Standard says in 13.4/7 that:

there are no standard conversions (clause 4) of one pointer-to-function type into another. In particular, even if B is a public base of D, we have

D* f();
B* (*p1)() = &f;  // error
void g(D*);
void (*p2)(B*) = &g;  // error

Still you may be could use function adapter for storing pointers to function with one argument, something like boost::function, but I'm not sure right now whether it will solve your problem.

Kirill V. Lyadvinsky
Howw, okay :( Thank you for the reference!
Aurélien Vallée
Even boost::function won't work in his case because TimerContext* is not implicitly convertible to MyTimerContext*. I mentioned this type-safety issue in my answer but I got downvoted for some reason.
sellibitze
A: 

Of course, casting function pointers is generally a bad idea.

Casting a function pointer from void(*)(Derived*) to void(*)(Base*) might work or it might not. It certainly won't work in case the internal representation of Derived* and Base* pointers needs to be adjusted upon conversion. However, in case of your single inheritance relationship, this is not likely. Still, class layout and pointer adjustments are implementation-defined and you should not rely on this. If you want to take the risk: Go ahread.

Assuming pointers don't need to be adjusted, a conversion from void(*)(Derived1*) to void(*)(Base*) would still not be a good idea (not type-safe) because it allows a function that expects Derived1* to be called with a Derived2* where Derived1 and Derived2 are siblings in the inheritance hierachy.

sellibitze
It's not implementation defined, it's undefined behavior (which has somewhat different implications).
Pavel Minaev
No, you misunderstood. class layout and possible pointer adjustments is implementation-defined.
sellibitze
A: 

reinterpret_cast is only as safe if the 'sender' and 'receiver' of the object correspond. So if sender and receiver are implemented by the same piece of code, it might be quite safe, for some time.

If you want to add callbacks to a delegate, and you want them to have a different type, as you do, you have two scenario's:

  • you know all the delegates upfront, at compile time => you can wrap them in a typelist.

  • delegates can be made up at run time => you need to use runtime binding, i.e. virtual functions ( an exec method or the like ).

xtofl