10

I'm trying to correctly use a forward declaration for enums. Therefore I searched the Internet, but I can't find something that works.

I'm using this in a header:

// Forward declaration
enum myEnumProcessState;

I'm then using this enum in a struct:

struct myStruct {
    [...]
    myEnumProcessState osState;
    [...]
};

And in another header:

enum myEnumProcessState {
    eNotRunning,
    eRunning
};

I found out that the type should be put in the enum forward declaration to be accepted. However, I don't know which "type" I should put for a Process State. These don't work:

enum myEnumProcessState : unsigned int;
enum myEnumProcessState : String;

I wanted to skip the forward declaration, but my struct is crying since it can't find it any more...

So I'm a bit confused. Is there a solution?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • why do you need to forward declare it? can't you just define it before you use it (that is, include the file that has the enum / move the enum to a file that can be included by both files)? – default Feb 28 '13 at 17:00
  • Well ... it DOES work. However I really would have wanted to do it with a forward declaration. Now, if the only "clean" solution is to skip it, I will ... –  Feb 28 '13 at 17:04
  • I actually don't know if there is a solution to this.. I however fail to see the reason why you would want to do it.. – default Feb 28 '13 at 17:08

1 Answers1

19

Before C++11, C++ didn't support forward-declaration of enums at all! However, some compilers (like MS Visual Studio) provide language extensions for that.

If your compiler doesn't support C++11, look in its documentation on enum forward declarations.

If you can use C++11, there is the enum class syntax (you almost got it right, but pay attention to the additional class keyword:

// Forward declaration
enum class myEnumProcessState: unsigned int;

// Usage in a struct
struct myStruct {myEnumProcessState osState;};

// Full declaration in another header
enum class myEnumProcessState: unsigned int {
    eNotRunning,
    eRunning
};

// Usage of symbols (syntax may seem slightly unusual)
if (myObject.osState == myEnumProcessState::eNotRunning) {
    ...
}
Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • Hm. When I put this, I got a "error : expected '{' before ';' token on the ligne "enum class myEnumProcessState : unsigned int;" ... –  Mar 01 '13 at 07:51
  • It seems your compiler supports c++03 but not c++11. Forward declarations for enums are not supported in c++03, but some compilers do support them as a language extension (whether or not you should use these extensions is a whole other can of worms). – anatolyg Mar 03 '13 at 11:02
  • It's G++ 4.7.2, I thought it followed C++11 by default. Doesn't it ? –  Mar 04 '13 at 09:53
  • 3
    Well ... it doesn't. I added -std=c++11 and now it does –  Mar 04 '13 at 10:13
  • 1
    `enum class` is not required for this, and nor is it always desirable. A plain `enum` can be forward-declared so long as the underlying type is specified. Having said that, the precise details are currently unclear to me. You'd assume a plain enum declared without an explicit underlying type would get `int`, but I'm getting a "type mismatch" error... not that it's kind enough to tell me what type it _thinks_ my original untyped declaration was specifying. :S Presumably, explicitly specifying the underlying type at both the full and forward declaration sites is safest/required. – underscore_d Nov 27 '16 at 15:59