strongly typed enums

Hi, how can I forward declare a strongly typed enum? I'm getting a lot of compiler issues when I try the below...

1
2
enum class exampleEnum : short;
enum class exampleEnum;


Thanks for any help.
Enum base shall coinside. So you should write

1
2
enum class exampleEnum : short;
enum class exampleEnum : short;


that is in the both declarations there shall be type specifier short.
Sorry, my post was misleading. I had meant that I have tried both of those lines, but only one at a time. For example, I tried this...

enum class exampleEnum;

and then this (after removing the above line)

enum class exampleEnum : short;


Neither of which worked.
It is not clear what is the problem.
Maybe your compiler does not support scoped enumerations.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum class animal : short ; // declaration
const char* common_name(animal) ;

// animal a = animal::BUTTERFLY ;  // *** error, animal has not been defined

enum class animal : short { AARDVARK, BUTTERFLY, COYOTE } ; // definition

animal a = animal::BUTTERFLY ;  // fine, animal has been defined

const char* common_name( animal a )
{
    switch(a) // fine, animal has been defined
    {
        case animal::AARDVARK : return "Aardvark" ;
        case animal::BUTTERFLY : return "Butterfly" ;
        case animal::COYOTE : return "Coyote" ;
        default : return "This is terrible" ;
    }
}

enum class animal : short ; // declaration 

Do you have C++11 enabled on your compiler?

If you have compiler errors - you should post them.
Last edited on
Hi, to elaborate on this... #includes, namespaces, are not included in below code pastes.

I do not get a compiler error when I do this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
enum class example : short
{
    exampleOne,
    exampleTwo,
    exampleThree
};
void function(example e);


int main()
{
    example ex = example::exampleTwo;
    function(ex);
    return 0;
}

void function(example e)
{
    cout << "function called. example: " << static_cast<short>(e) << endl;
}



However, if I move the function dec to look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void function(example e);

enum class example : short
{
    exampleOne,
    exampleTwo,
    exampleThree
};

int main()
{
    example ex = example::exampleTwo;
    function(ex);
    return 0;
}

void function(example e)
{
    cout << "function called. example: " << static_cast<short>(e) << endl;
}


I get the following compile errors:
1
2
3
4
main.cpp:4: error: variable or field 'function' declared void
main.cpp:4: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:16: error: 'function' was not declared in this scope



Ill post the forward declarations I tried in the next post to separate it
If I try a forward declaration like this...

enum class example;

I get the following compilation errors.

1
2
3
4
5
main.cpp:4: error: use of enum 'example' without previous declaration
main.cpp:5: error: variable or field 'function' declared void
main.cpp:5: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:17: error: 'function' was not declared in this scope


And when I try to do a forward declaration like this:


enum class example : short;

I get the following compilation errors:

1
2
3
4
5
6
main.cpp:4: error: expected '{' before ';' token
main.cpp:4: error: expected class-key before ';' token
main.cpp:5: error: variable or field 'function' declared void
main.cpp:5: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:17: error: 'function' was not declared in this scope


However, if I reorder it, and dont attempt a forward declaration of the strongly typed enums, like this, it works perfectly (but is restrictive).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
enum class example : short
{
    exampleOne,
    exampleTwo,
    exampleThree
};

void function(example e);


int main()
{
    example ex = example::exampleTwo;
    function(ex);
    return 0;
}

void function(example e)
{
    cout << "function called. example: " << static_cast<short>(e) << endl;
}
Last edited on
Please show the full code snip when you are doing the forward declaration of the enum and are getting errors

main.cpp:4: error: expected '{' before ';' token
main.cpp:4: error: expected class-key before ';' token
main.cpp:5: error: variable or field 'function' declared void
main.cpp:5: error: 'example' was not declared in this scope
main.cpp: In function 'int main()':
main.cpp:17: error: 'function' was not declared in this scope
This is the full code which gives the errors you posted:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

enum class example : short; // attempting a forward declaration of the strongly typed enum

void function(example e);

enum class example : short
{
    exampleOne,
    exampleTwo,
    exampleThree
};

int main()
{
    example ex = example::exampleTwo;
    function(ex);
    return 0;
}

void function(example e)
{
    cout << "function called. example: " << static_cast<short>(e) << endl;
}
It looks like that you are using MS VS 2010 and C++/CLI. So your code is not a native C++ code.
I have tried compiling the code under...

mingw/g++ in windows
g++ in debian
clang++ in debian

With no success. The appropriate c++0x flags were used.
Your code compiles cleanly (and runs) with GCC 4.7 and clang 3.2 .

What were the GCC and clang versions that you used?
Just checked. Outdated by a fair amount. That more or less solves my problem, thanks for the pointer (no pun intended)
Topic archived. No new replies allowed.