[Bug] HeapSort with TArray of pointers fails to compile

Hi, this example code fails to compile:

struct FMyObject
{
	int32 Number;
};

///////////////////////////////////////////////////////////////////////////////

struct FMyObjectComparator
{
	FORCEINLINE bool operator()(const FMyObject& A, const FMyObject& B) const { return A.Number < B.Number; }
};

///////////////////////////////////////////////////////////////////////////////

void SomeCode()
{
	TArray<FMyObject>	SolidObjects;

	SolidObjects.Heapify(FMyObjectComparator());	// compiles fine
	SolidObjects.HeapSort(FMyObjectComparator());	// compiles fine

	TArray<FMyObject*>	ObjectPtrs;

	ObjectPtrs.Heapify(FMyObjectComparator());		// compiles fine
	ObjectPtrs.HeapSort(FMyObjectComparator());		// ERROR CITY
}

Is this a known issue? Is there a workaround?

Many thanks,
-.

(originally posted here: HeapSort with TArray of pointers fails to compile - C++ - Epic Developer Community Forums)

Hi,

That is indeed a bug. I’ve now fixed it in our codebase, but here are the changes you can use to workaround it now:

  • Rename the Heapify function to HeapifyInternal.

  • Remove the PredicateWrapper object from HeapifyInternal and replace its two uses with Predicate.

  • Change the Heapify call inside the predicated HeapSort to HeapifyInternal.

  • Add a new Heapify function like this:

    template
    FORCEINLINE void Heapify(const PREDICATE_CLASS& Predicate)
    {
    HeapifyInternal(TDereferenceWrapper<ElementType, PREDICATE_CLASS>(Predicate));
    }

This should get you compiling again.

Sorry for the trouble,

Steve

I’ll give that a go, many thanks :slight_smile: