• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 439
  • Last Modified:

enum (high, low)

if I define:
enum COLORS {
How to find programaticaly (in C or C++) what is the lowest and the highest member of COLOR? Something like Delphi Pascal's function High, which returns "green" and function Low which returns "red" for the above enum.
1 Solution

In C, I do not believe that you can.

This is simply because an enum is basically an
int with the enums simply acting as if you had
given them numbers via #defines.

The main advantage of enums besides readability
is that values can come up literally in a debugger.

I am not a C++ genius, but I do not think you can
do it there either.

The difference in C++ from C with enums is that
you can assign an enum variable an integer  number
in C, but not in C++ where you have to use a symbolic
constant in the assignment statement.  I do not
know of a way to do you want but you can get tricky
by always defining a special terminator name such
as colors_last at the end of your enum definition.  Then
you could test its value.


The terminators are the only way I know of in C++.
enum's have another advantage, at least in C++; any parameter that is expected to be of a specific enum type is checked for that type. So

void f(COLOR parm);

void g()
  f(0); // is an error
  f(COLOR::red); // is ok

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now