Creating a function that accepts a predicate function

Hi all,

I’m trying to create a function that accepts a predicate, similar to TArray::FindByPredicate.

The thing is I can’t see how FindByPredicate defines the signature of the function it accepts.

For example I want to accept a function that has a signature bool (const Node& A, const Edge& E)

How can I do this? I’m trying to do it in pure C++, no blueprint stuff.

TArray::FindByPredicate doesn’t explicitly define what it accepts, and uses templates to accept any callable type that returns something truthy and accepts an array item as an argument.

You’re probably looking for TFunctionRef (if your function only needs a reference to an outer function, like a typical search predicate), or TFunction (if your function needs the instance to outlive the caller).

Wow thanks for the reply!
I looked into tfunction and tfunctionptr but I couldn’t find enough doc to work out how to use them with the C11 style anonymous function thingies?
Could you show an example? Sorry ^^;

Do you mean a lambda? They should just implicitly convert.

void MyFunc(TFunctionRef<bool(const Node&, const Edge&)> Pred)
	// Call Pred

MyFunc([](const Node& A, const Edge& E) {
	return true;

Docs are here:

1 Like