views:

61

answers:

1

I am using VC2008 as my complier, and it is surprised to me that an enum could be used without defined:

void func(enum EnumType type)
{

}

Code above could be compiled and run without a problem, could anyone explain why it works?

Update: I could define an empty enum in C++, as follow:

enum EnumType {};
+1  A: 

This is evidently a nonstandard Visual C++ language extension.

You cannot forward declare an enum in standard C++.

James McNellis
Do you mean it consider the expression "enum EnumType type" as forward declaration?
lz_prgmr
@Dbger: Yes.***
James McNellis
If this is ture, seems MS makes 2 tricks here: 1. You can forward declare sth when you declare a function's parameter; 2. VC++ considers the enum's size should always be 4 bytes, at least as default.
lz_prgmr
@Dbgr: You can forward declare a class in a function declaration: `void f(class C)`. If I recall correctly, the type underlying an `enum` in Visual C++ is always `int` (it's either that or `unsigned`; I really can't remember).
James McNellis
@Dbger: Number two is required(-ish). An empty enum is treated, storage wise, as if it had an enum with the value zero. And the underlying type of an enum is unspecified, but cannot be larger than an `int` unless the values of the enumerations requires it to be. MS just chooses `int` is the default underlying type, which is both okay and a good idea. And `sizeof(int)` is 4, in this case.
GMan