开发者

Use Enum or #define?

开发者 https://www.devze.com 2022-12-25 07:51 出处:网络
I\'m building a toy interpreter and I have implemented a token class which holds the token type and value.

I'm building a toy interpreter and I have implemented a token class which holds the token type and value.

T开发者_运维百科he token type is usually an integer, but how should I abstract the int's?

What would be the better idea:

// #defines
#define T_NEWLINE 1
#define T_STRING 2
#define T_BLAH 3

/**
 * Or...
 */

// enum
enum TokenTypes
{
   t_newline = 1,
   t_string = 2,
   t_blah = 3
};


Enums can be cast to ints; furthermore, they're the preferred way of enumerating lists of predefined values in C++. Unlike #defines, they can be put in namespaces, classes, etc.

Additionally, if you need the first index to start with 1, you can use:

enum TokenTypes
{
   t_newline = 1,
   t_string,
   t_blah
};


Enums work in debuggers (e.g. saying "print x" will print the "English" value). #defines don't (i.e. you're left with the numeric and have to refer to the source to do the mapping yourself).

Therefore, use enums.


There are various solutions here.

The first, using #define refers to the old days of C. It's usually considered bad practice in C++ because symbols defined this way don't obey scope rules and are replaced by the preprocessor which does not perform any kind of syntax check... leading to hard to understand errors.

The other solutions are about creating global constants. The net benefit is that instead of being interpreted by the preprocessor they will be interpreted by the compiler, and thus obey syntax checks and scope rules.

There are many ways to create global constants:

// ints
const int T_NEWLINE = 1;

struct Tokens { static const int T_FOO = 2; };

// enums
enum { T_BAR = 3; }; // anonymous enum

enum Token { T_BLAH = 4; }; // named enum

// Strong Typing
BOOST_STRONG_TYPEDEF(int, Token);
const Token NewLine = 1;
const Token Foo = 2;

// Other Strong Typing
class Token
{
public:
  static const Token NewLine; // defined to Token("NewLine")
  static const Token Foo;     // defined to Token("Foo")

  bool operator<(Token rhs) const { return mValue < rhs.mValue; }
  bool operator==(Token rhs) const { return mValue == rhs.mValue; }
  bool operator!=(Token rhs) const { return mValue != rhs.mValue; }

  friend std::string toString(Token t) { return t.mValue; } // for printing

private:
  explicit Token(const char* value);

  const char* mValue;
};

All have their strengths and weaknesses.

  • int lacks from type safety, you can easily use one category of constants in the place where another is expected
  • enum support auto incrementing but you don't have pretty printing and it's still not so type safe (even though a bit better).
  • StrongTypedef I prefer to enum. You can get back to int.
  • Creating your own class is the best option, here you get pretty printing for your messages for example, but that's also a bit more work (not much, but still).

Also, the int and enum approach are likely to generate a code as efficient as the #define approach: compilers substitute the const values for their actual values whenever possible.


In the cases like the one you've described I prefer using enum, since they are much easier to maintain. Especially, if the numerical representation doesn't have any specific meaning.


Enum is type safe, easier to read, easier to debug and well supported by intellisense. I will say use Enum whenever possible, and resort to #define when you have to.

See this related discussion on const versus define in C/C++ and my answer to this post also list when you have to use #define preprocessor.

Shall I prefer constants over defines?


I vote for enum

#define 's aren't type safe and can be redefined if you aren't careful.


Another reason for enums: They are scoped, so if the label t_blah is present in another namespace (e.g. another class), it doesn't interfere with t_blah in your current namespace (or class), even if they have different int representations.


enum provided type-safety and readability and debugger. They are very important, as already mentioned.

Another thing that enum provides is a collection of possibilities. E.g.

enum color
{
  red,
  green,
  blue,
  unknown
};

I think this is not possible with #define (or const's for that matter)


Ok, many many answers have been posted already so I'll come up with something a little bit different: C++0x strongly typed enumerators :)

enum class Color /* Note the "class" */
{
   Red,
   Blue,
   Yellow
};

Characteristics, advantages and differences from the old enums

  • Type-safe: int color = Color::Red; will be a compile-time error. You would have to use Color color or cast Red to int.
  • Change the underlying type: You can change its underlying type (many compilers offer extensions to do this in C++98 too): enum class Color : unsigned short. unsigned short will be the type.
  • Explicit scoping (my favorite): in the example above Red will be undefined; you must use Color::Red. Imagine the new enums as being sort of namespaces too, so they don't pollute your current namespace with what is probably going to be a common name ("red", "valid", "invalid",e tc).
  • Forward declaration: enum class Color; tells the compiler that Color is an enum and you can start using it (but not values, of course); sort of like class Test; and then use Test *.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号