Is there a way to use enum class entries as array indexes without casting?

Hello, i have the following enum class:

UENUM(BlueprintType)
enum class ETest : uint8
{
	Option1,
	Option2,
	Option3
};

I also have a TArray Options. I want to use the enum’s values as an index, like:

Options[ETest::Option1];

but he gives an error. Only if i put a cast does this work:

Options[(int32)ETest::Option1];

I don’t have this problem on regular C++ enums and namespace enums. Any suggestions? Thank you :slight_smile:

Hi,

No, this is a C++ issue rather than something specific to Unreal or TArray. Enum classes are strongly typed and don’t support the usual integral promotion associated with regular enums, and are thus unusable as array indices (which are integral).

Steve

Do you recommend using namespace enums or enum classes then?

We don’t recommend anything in particular, but we support both styles. That said, we will likely migrate engine enums across to enum classes over time for consistency and explicitness.

It’s really up to you. If you feel the explicit cast is troublesome to write every time, use a namespaced enum. Otherwise choose an enum class.

Steve

Thank you very much for your answer :slight_smile: