USTRUCT with type T

I’m trying to make a USTRUCT with a type T:

template<typename T>
USTRUCT()
struct MY_API FOutcome
{
	GENERATED_BODY()

public:
	FOutcome() { Success = false; Result = NULL; }
	FOutcome(bool success, T result) { Success = success; Result = result; }

	UPROPERTY(EditAnywhere)
	bool Success ;

	UPROPERTY(EditAnywhere)
	T Result;
};

But it fails to compile whenever I include the template line. If I can’t use template types for structs, what’s the next simplest form of class I can use them with? (UOBJECT, UCLASS etc)

Unfortunately you aren’t able to use template with any of the unreal UObjects, and by extension pretty much anything Unreal directly cares about- no UPROPERTY, no UFUNCTION, no UCLASS, no USTRUCT.

You can however use template functions- though they can’t be wrapped in a UFUNCTION, they can be called from within a UFUNCTION.

So to answer your question directly, the next form of class you can use is the normal C++ class/struct.
I’m not sure what you’re doing, but you might be able to just store a UObject* instead- basically everything inherits from them, including DataAssets, so that can fulfill most use cases.
Alternatively, you might be able to use std::any (#include <any>).

Appreciate the informative answer!

I can use a UObject but I assume I’d need to cast everytime? (which I assume has some performance hit)

I’m actually just trying to make a simple object which can be used to represent whether something was successful or not. For example if a Tree is asked to give fruit, but there is none left, it felt strange to have it return NULL.

You would need to cast any time you want to use whatever specific object you’re saving, at least for the unique functionality that isn’t already in the UObject itself. If you use static_cast, there is literally no performance overhead, but if you do runtime casting with type checking there is a minuscule overhead (it’s basically a single if statement).

In that case, why not just use a boolean or enum, rather than a dedicated struct? Though maybe I’m missing the purpose of the return object.