Reputation: 64223
When passing forward declared struct or a class, one has to pass it to a function through a reference or a pointer.
But, what can be done with a forward declared enum? Does it also have to be passed through a reference or a pointer? Or, can it be passed with a value?
Next example compiles fine using g++ 4.6.1 :
#include <iostream>
enum class E;
void foo( const E e );
enum class E
{
V1,
V2
};
void foo( const E e )
{
switch ( e )
{
case E::V1 :
std::cout << "V1"<<std::endl;
break;
case E::V2 :
std::cout << "V2"<<std::endl;
break;
default:
;
}
}
int main()
{
foo( E::V1);
foo( E::V2);
}
To build :
g++ gy.cpp -Wall -Wextra -pedantic -std=c++0x -O3
Is the above standard compliant, or is it using an extension?
Upvotes: 9
Views: 354
Reputation: 234394
A declared enum, even if you don't specify the enumerators (what the standard calls an opaque-enum-declaration) is a complete type, so it can be used everywhere.
For completeness, here's a quote from paragraph 3 of §7.2:
An opaque-enum-declaration is either a redeclaration of an enumeration in the current scope or a declaration of a new enumeration. [Note: An enumeration declared by an opaque-enum-declaration has fixed underlying type and is a complete type. The list of enumerators can be provided in a later redeclaration with an enum-specifier. —end note ]
And the grammar for opaque-enum-declaration, from paragraph one of the same §7.2:
opaque-enum-declaration:
enum-key attribute-specifier-seqopt identifier enum-baseopt
;
Upvotes: 11