How to check if an enum variable is valid?

匿名 (未验证) 提交于 2019-12-03 08:35:02

问题:

I have an enum:

enum myenum{   typeA,   typeB,   typeC } myenum_t;

Then, a functions is to be called with an enum parameter:

int myfunction(myenum_t param1) {   switch(param1)   {     case typeA:     case typeB:     case typeC:       //do the work       break;      default:       printf("Invalid parameter");   }   return 0; }

But, as myenum_t grows with more and more values, myfunction doesn't seem so elegant.

Is there a better way to check if an enum is valid or not?

回答1:

A common convention for this is to do something like this:

typedef enum {   typeA,   typeB,   typeC,   num_types } myenum_t;

Then you can check for (t < num_types).

If you subsequently add more enums, e.g.

typedef enum {   typeA,   typeB,   typeC,   typeD,   typeE,   num_types } myenum_t;

then num_types is automatically updated and your error checking code doesn't need to change.



回答2:

You could use a bitwise enum:

enum myEnum {     typeA = 1 << 0;     typeB = 1 << 1;     typeC = 1 << 2; }  int myFunction(myEnum arg1) {     int checkVal = typeA | typeB | typeC;      if (checkVal & arg1)     {         // do work here;     }     else     {         printf("invalid argument!");     }      return 0; }

Excuse me, it seems I have read the question wrong.

It appears what you want to do is determine if you have a proper value passed in, not some random invalid option. In that case, the most logical option is this:

if (arg1 < typeA || arg1 > typeC)     printf("invalid argument");

This is, of course, assuming you don't set manual values for your enum, which, is quite rare unless using bitwise enums.



回答3:

Yes.

Let the compiler do its work, don't cast int to an enum type and you should be good.



回答4:

One trick I've used in the past:

enum foo {FIRST_FOO, BAR, BLETCH, BLURGA, BLAH, LAST_FOO};

and then check to see if your value is > FIRST_FOO && < LAST_FOO1.

Of course, this assumes that there are no gaps between your enumeration values.

Otherwise, no, there's no good method to do what you're asking (at least in C).


1 From the latest online C Language Standard:
6.7.2.2 Enumeration specifiers
...
3 The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted.109) An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.) The enumerators of an enumeration are also known as its members.


回答5:

Can't you also do something like

enum myEnum {typeA,typeB, typeC};  int myFunction (myEnum arg1) {     if (arg1 >= typeA && arg1 <= typeC) {         // do work here     } else {         printf("invalid argument!");     }     return 0; }


回答6:

Unfortunately there isn't a simple way to do it language level (at least with C), you just have to make sure you are using only variables defined via enum.

Although you could enable one of the following compiler warnings together with -Werror:

  • -Wswitch
  • -Wswitch-default
  • -Wswitch-enum

This makes build fail if one of the enums is missed inside switch.



回答7:

Enumerations in C++ already have stronger types than in C.

Take the following simple program:

#include <iostream>  enum E {     A,     B  };  void f(E e) { }  int main() {     f(1); }

Using the GCC compiler I will get a this error:

enum.cpp: In function int main()’: enum.cpp:15: error: invalid conversion from int to E enum.cpp:15: error:   initializing argument 1 of void f(E)’

So as you can see the enumeration members are already checked.

If you want even stronger type checking, and have a C++11 capable compiler, you can use even stronger type checking for enums, see http://en.wikipedia.org/wiki/C%2B%2B11#Strongly_typed_enumerations.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!