4

I saw the following in the header file

typedef enum Test__tag {
    foo,
    bar
} Test;

I am wondering why using typedef; I can just use

enum Test{
    foo,
    bar
};

is that right?

Richard
  • 6,812
  • 5
  • 45
  • 60
Adam Lee
  • 24,710
  • 51
  • 156
  • 236
  • 3
    Using the name `Test__tag` is not a good idea, because all names with double underscores are reserved for the implementation. – Zyx 2000 Jun 10 '13 at 09:22
  • @Zyx2000 I thought only those which *start* with a double underscore (or single underscore and captial letter). – Christian Rau Jun 10 '13 at 13:53
  • @Christian Rau All names with double underscores anywhere are reserved. See http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier, where the standard is quoted in the first answer. – Zyx 2000 Jun 10 '13 at 16:14

2 Answers2

11

It's for C users. Basically when you go the typedef way, in C you can say

Test myTest;

whereas when you just used enum Test, you'd have to declare a variable like this:

enum Test myTest; 

It's not needed if only C++ is used though. So it might also just be written by a C programmer who is used to this style.

Botz3000
  • 39,020
  • 8
  • 103
  • 127
3

Yes, in C++ you declare x without the typdef just by writing

 enum Test{
    foo,
    bar
};
Test x;

However if you were to try the same thing in C you would need to declare x as

 enum Test x;

or use the typedef.

So the the typedef is probably a habit left over from C (unless the header file is in fact C).

Bull
  • 11,771
  • 9
  • 42
  • 53