问题描述
当传递声明的struct或类时,必须通过引用或指针将它传递给一个函数。
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?
下一个示例使用g ++ 4.6.1编译良好:
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);
}
建立:
g++ gy.cpp -Wall -Wextra -pedantic -std=c++0x -O3
是否符合上述标准,还是使用扩展程序?
Is the above standard compliant, or is it using an extension?
推荐答案
,即使你没有指定枚举器(标准调用一个 opaque-enum-declaration )是一个完整的类型,因此它可以在任何地方使用。
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.
为了完整性,这里引用了§7.2第3段:
For completeness, here's a quote from paragraph 3 of §7.2:
,从同一§7.2的第一段:
And the grammar for opaque-enum-declaration, from paragraph one of the same §7.2:
这篇关于我可以通过转发已声明枚举的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!