Since you want to map enumerators to strings, C_ntua's example should actually be:
Code:enum name { Mike = 0, John }; // ... map<name, string> enumName; enumName.insert(make_pair(Mike, "Mike")); enumName.insert(make_pair(John, "John"));
Since you want to map enumerators to strings, C_ntua's example should actually be:
Code:enum name { Mike = 0, John }; // ... map<name, string> enumName; enumName.insert(make_pair(Mike, "Mike")); enumName.insert(make_pair(John, "John"));
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
We are all saying unsigned int b/c enums are really unsigned int's under the hood. They are not 'technically' the same thing to the compiler as laserlight's example shows.
Here is a really poorly named map that maps ints to strings. No dynamic memory allocation in my class. That is not to say the map under the hood is not dynamically allocating memory but I really don't care b/c the beauty of the STL is...um...that it works and it's all transparent to me. There are special cases when using pointers in containers but this example doesn't require any of that.
Code tested and compiled in MSVC 2005.
Last edited by VirtualAce; 03-12-2011 at 11:41 AM.
Strictly speaking, the underlying type of an enumerator value is an implementation defined choice that depends on all the enumerators in an enumeration. This is why I chose to use the enumeration type itself rather than unsigned int in my example. Then there is also the issue readability: it is kind of silly to use magic numbers when one of the reasons for using an enum is to have named constants.Originally Posted by Bubba
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Not so fast...
Yes logically a comma separates things and there is no need to separate the last thing from the nothing that follows.
However, this is intentionally accepted by the compiler and for good reason:
This is because it makes it easier to add an additional value later. Specifically since you don't have to alter the previous line to add the comma, a version control system doesn't indicate a change was made to the previous line. It also means reordering lines is easier as you don't have to worry about one line not having a comma.
It is very common and is indeed even considered good practice by many. It is done both with enums and array initialisation data.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
To be honest, I thought the same, but when checking, I realised that I was wrong: unlike aggregate initialiser lists, a trailing comma is not allowed for enumeration declarations. This is why I removed that comma in my example.Originally Posted by iMalc
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
For the record, stainedBlue was quoting what I said in the first post, and it seems having a comma after the last enum value in an enum doesn't work in all cases, because I've tried it before with the gcc compiler in Ubuntu, and it wouldn't compile. That is why I assumed that you're always not supposed to put a comma after the last enum value.
@Bubba: I can't tell if you're recommending that I change my program to use maps, or if you're recommending to use maps in place of my program. I'm willing to change my program to use maps, but I don't intend to use maps instead of my program.
And I don't care what you do with maps...it doesn't change the fact that you still end up having to manually type up the string names of the enum values if you don't use my program.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Pair and make_pair is in <utility>. Include that just to be sure.
What's done is done, obviously. I have code that works that I frequently use that I'd still love to re-write to work better and/or differently, but I don't have time. I'm sure that's true of most people.
I think the idea here was not necessarily to force you to scrap everything and start again, but more to point out the possibilities that you missed so you are aware of them in the future.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Yeah, but it wouldn't take me that long to change it to use maps instead if I wanted to, then re-upload it, and get Bubba to change the download link. Like already mentioned, I'm willing to do this if its such a big issue.
But my program works, as is, right now.
I do understand people's misconceptions about the program though. I forgot to mention in the first post that the program does more than just converts any enum. What my program does that makes it more valuable than that is it provides a function which returns a reference to a vector of enum value strings (automatically generated by my program). This makes it possible to get all the enum value names without typing them up. Thus, if you have a really long enum, for example, which has many enum values, and you don't want to type up each string name of those enum values, you can use my program to do it for you.