"Unscoped" vs "Scoped" enums

What is the difference between scoped enums and unscoped enums, and how does the syntax differ?

A scoped enum is declared inside a scope, such as a class or a namespace.



class MyClass
{
    enum MyEnum
    {
        ValueOne,
        ValueTwo
    }
};


You would use it like this



MyClass::ValueOne


vs a non-scoped enum, which you would use like this



ValueOne


So you have to put the name of the scope that it was declared in first.

1 Like

I think most folks tend to use class-based enums now instead, since it prevents having to use the TEnumAsByte workaround:



UENUM()
enum class EMyEnum : uint8
{
    EME_None,
    EME_One,
    EME_Two,
};


I am using that setup, but UBT keeps saying
Error d:\library storage\documents\unreal projects\distanthome\source\distanthome\Public/GameInfo.h(18) : error C3431: ‘EWeaponSlot’ : an unscoped enumeration cannot be redeclared as a scoped enumeration

UBT complains about “cannot be redeclared”. Looks like you have enum with same name or same values’ names somewhere else.

Yeah It’s possible that you’re either redeclaring it, or it doesn’t know what it is.

i tend to use the


enum class : uint8

setup, since its gives me the opportunity to even forward declare enums in other classes as well.

Also i dislike the Scope::Type version you see sometimes, because reading the scope with an “E” prefix is enough for a user to know that he is confronted with an enum, so the additional Type and
the writing of that enum like: Scope::Type::Value is unnecessary additional information.

you could even suggest that there is more than just ~::Type in that scope.

Btw. there is a difference in intention for using a namespace or a class as the scope for an enum FMPOV.

if you use a namespace as a scope like:




namespace ESomeEnum
{
    enum Type
    {
        ONE,
        TWO
    }
}



you will expose this enum to everyone which includes this file.

On the other hand if you use it within a class like:



class MyClass
{
private:
	enum EClassInternalEnumeration
	{
		One,
		Two
	};
};


you are able to nest that enum inside the class, and even make it private to prevent someone from the outside to see that enum.

Im sure one of the master-programmers will give you even more useful tips and information about enums, so consider me as a
small fry which tries to share his knowledge with you :wink:

Hey never used the class way for enums.
But if its giving you problems is how you can set them up.


UENUM(BlueprintType, Category = "MY Enums")
namespace EMyEnum
{
	enum Type
	{
		EMY_None		UMETA(DisplayName = "Undefined My Enum Type"),
		EMY_Enum_One		UMETA(DisplayName = "My Enum One")
	};
}


Then a member be decleard like this.


TEnumAsByte<EMyEnum::Type> MyEnumType;

Also if you need to pass it into e.g: RPC Method.
You can type cast the enum from a TEnumAsByte like so.



// From TEnumAsByte to uint8.
((uint8)MyTEnumMember);

// From uint8 to TEnumAsByte<EMyEnum::Type>
((EMyEnum::Type)MyEnumAsUint)


Hope this helps you out.

1 Like