Didn't know enum classes existed in C++

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Pindrought
Posts: 432
Joined: September 26th, 2013, 4:57 pm
Location: Kentucky
Contact:

Didn't know enum classes existed in C++

Post by Pindrought » June 9th, 2016, 12:39 am

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;
}
PM me if you need to contact me. Thanks to all the helpful people on this forum especially to Chili.

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

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

Post by chili » June 9th, 2016, 1:10 am

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.
Chili

Pindrought
Posts: 432
Joined: September 26th, 2013, 4:57 pm
Location: Kentucky
Contact:

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

Post by Pindrought » June 9th, 2016, 3:26 am

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.
PM me if you need to contact me. Thanks to all the helpful people on this forum especially to Chili.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

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

Post by albinopapa » June 9th, 2016, 3:37 am

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.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
cyboryxmen
Posts: 190
Joined: November 14th, 2014, 2:03 am

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

Post by cyboryxmen » June 10th, 2016, 4:13 am

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
Zekilk

Post Reply