Page 1 of 1

Didn't know enum classes existed in C++

Posted: June 9th, 2016, 12:39 am
by Pindrought
So apparently with C++11 there are enum classes now.

Enums can by default work like this in C#, but I didn't know that C++ had a similar feature without manually creating a class with enums inside and overriding the operators, etc.

I realize this might not be anything new to some people on here, but thought it was worth posting since it's just nice for organizing your enums better.

Code: Select all

enum class FontWeight
{
	Bold,
	Normal
};

int main()
{
	FontWeight test = FontWeight::Bold;
	return 0;
}

Re: Didn't know enum classes existed in C++

Posted: June 9th, 2016, 1:10 am
by chili
Yeah, it makes enum typing stricter and also prevents the enum symbols from polluting the namespace of an enclosing class or whatever (I think). I don't use it because I like myclass::myenumsymbol better than myclass::myenum::myenumsymbol.

Re: Didn't know enum classes existed in C++

Posted: June 9th, 2016, 3:26 am
by Pindrought
chili wrote:Yeah, it makes enum typing stricter and also prevents the enum symbols from polluting the namespace of an enclosing class or whatever (I think). I don't use it because I like myclass::myenumsymbol better than myclass::myenum::myenumsymbol.

Fair enough. I just really like the strict typing. I'm going to start using this, but mainly for when i'm working on a large project and take a break from it for a few months. With the old way I was doing things, i'll find myself coming back and forgetting all of the different things I named my enums, and I think it would be a bit easier(lazier ^^) to just let intellisense tell me all of the possible options and to have certain variables restricted to the possible enum values in the case that i'm coming back to a project that I am no longer familiar with.

But yeah it does seem to call for some excessively long names.

Re: Didn't know enum classes existed in C++

Posted: June 9th, 2016, 3:37 am
by albinopapa
Yes chili you are correct from my experience with them. You have to scope to the symbols explicitly. One reason I like this is intellisence, being able to only have the list of available members, methods and enums in the list is quite nice. So if you have several enum types in a namespace, it's nice to be able to just have the enum types then from there only have the enumerations for that scoped type.

For those that don't use intellisence or already know what symbols are available and what not, then I suppose it would get annoying to have to scope to the enumerations each time.

Hehe, tried posting this, but you beat me to it Pindrought, it seems you and I are of like minds on this one.

Re: Didn't know enum classes existed in C++

Posted: June 10th, 2016, 4:13 am
by cyboryxmen
The scoping is one of the advantages of enum classes. Enums are primary used for switch statements because it's a good way to describe in code which test case you are going to call. Enum classes add a level of type safety to your code by making them actual types rather than fancy numbers preventing you from doing math functions with them. It's a minor safety measure but goes a long way from preventing people from using enums the way it is not supposed to be used without realising. Now they must make a conscious effort to convert the enum into numbers or better yet, use an actual number instead of an enum(albeit, generating a random enum for your program seems to still be a legitimate reason to do math with enums).
-Zekilk