我可以通过转发已声明枚举的值吗? [英] Can I pass value of forward declared enum?

查看:163
本文介绍了我可以通过转发已声明枚举的值吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当传递声明的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:


一个 opaque-enum- 是当前范围中的枚举
的重新声明,或者是新枚举的声明。 [注意: opaque-enum-declaration 声明的
枚举已固定
底层类型,并且是完整类型。枚举器列表可以是在稍后的重新声明中使用枚举说明符提供的
-end note ]

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 ]

,从同一§7.2的第一段:

And the grammar for opaque-enum-declaration, from paragraph one of the same §7.2:


opaque-enum-声明: b
$ b

opaque-enum-declaration:


enum-key attribute-specifier-seq opt 标识符枚举基本 ;


这篇关于我可以通过转发已声明枚举的值吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆