Template in TArray

Hello.

I’ve been trying to make a function take a TArray with my template typename as an argument, but it seems as if it’s not possible.

Here is my function:



	template<typename T>
	static FORCEINLINE T* GetAssetFromAssetLibrary(const TArray<T*> AssetLibrary, const FString& AssetID)
	{
		for (const UObject* Asset : AssetLibrary)
		{
			T* AssetPtr = Cast<T>(Asset);
			if(!AssetPtr)
				continue;

			if (AssetPtr == AssetID)
				return AssetPtr;
		}

		return nullptr;
	}

This is the error I get:


Error	C2664	'T *UStaticLib::GetAssetFromAssetLibrary<T>(const TArray<T *,FDefaultAllocator>,const FString &)': cannot convert argument 1 from 'TArray<UTestAsset *,FDefaultAllocator>' to 'const TArray<T *,FDefaultAllocator>'

Any ideas? :slight_smile:

Should work;
I have done this before and for me compiles.
Btw, try changing:



static FORCEINLINE T* GetAssetFromAssetLibrary(const TArray<T*> AssetLibrary, const FString& AssetID)


to:



static FORCEINLINE T* GetAssetFromAssetLibrary(const TArray<UObject*> AssetLibrary, const FString& AssetID)


And cast UTestAsset to UObject when passing to the function.

Hm. Now the problem seems to be that TArray<UTestAsset*> cannot be converted to TArray<UObject*> without casting UTestAsset to UObject beforehand.
I wanted to cast it beforehand if I could cast an entire array with a single function call, but I couldn’t find any way to do that.

Do I have to loop through my UTestAsset array to cast and store it as a UObject array? Isn’t there a simpler way? :slight_smile:

Depends on what you want to do…
Could create static method to return fully cast array or maybe overload TArray operator to do that for you.