How can I convert a Delegate to TFunction?

void UMyObj::Func( int val )
{
fprintf( stderr, "val=%d
", val );
}

DECLARE_DELEGATE_OneParam( FFuncIntParam, int );

FFuncIntParam Delegate;
Delegate.BindUObject( this, &UMyObj::Func );

TFunction<void(int)> fp = Delegate; // this line is a error

I wanna do it just like std::bind:

class FooFunc {
public:
void func( int val )
{
fprintf( stderr, "val=%d
", val );
}

};

FooFunc foo;
std::function<void(int)> ff = std::bind( &FooFunc::func, &foo, std::placeholders::_1 );
ff( 5 );

thank u.

You can use a lambda


TFunction<void(int)> fp = [Delegate](int val) { Delegate.ExecuteIfBound(val); };

That will be a bit less efficient, but it’s probably the best you can manage if you want to do this will an arbitrary delegate.

If you don’t need the delegate and just want to bind a member function to a TFunction, you can do


TFunction<void(int)> fp = [this](int val) { Func(val); };

There’s probably a syntax for binding directly to a member function without the lambda, but I don’t know it.

1 Like

Thank u very much. I don’t wanna use lambda.

1 Like