At one point I had looked at implementing a class/template in C++ that would support an Enum that would behave like it does in Ada. It has been some time since I thought about this problem and I was wondering if anyone has ever solved this problem?
EDIT:
My apologies, I should clarify what functionality I thought were useful in the Ada implementation of the Enum. Given the enumeration
type fruit is (apple, banana, cherry, peach, grape);
We know that fruit is one of the listed fruits: apple, banana, cherry, peach, grape. Nothing really different there from C++.
What is very useful are the following pieces of functionality that you get with every enum in Ada without any additional work:
- printing out an enumerated value generates the string version
- you can increment the enumerated variable
- you can decrement the enumerated variable
I hope this defines the problem a bit more.
Notes added from comments:
Useful features of Ada enumerations
- The first value in the enumeration is
fruit'first
which givesapple
. - The last value in the enumeration is
fruit'last
which givesgrape
. - The increment operation is
fruit'succ(apple)
which givesbanana
. - The decrement operation is
fruit'pred(cherry)
which also givesbanana
. - The conversion from enumeration to integer is
fruit'pos(cherry)
which returns2
because Ada uses 0-based enumerations. - The conversion from integer to enumeration is
fruit'val(2)
which returnscherry
. - The conversion from enumeration to string is
fruit'Image(apple)
which returns the (upper-case) string"APPLE"
. - The conversion from string to enumeration is
fruit'Value("apple")
which returns the valueapple
.
See also related SO questions: