use of enum E without previous declaration 因为C++98标准中没有支持枚举类型前向声明,所以就不能保证上面的写法对任何编译器都行。为什么枚举类型不能被前向声明呢? 因为编译器无法知道枚举变量所需的存储空间。 在编译期,C ++编译器要为变量保留存储空间。 如果所有可见的都是前向声明,那么编译器无法知道选择哪...
使用-features=extensions时,编译器允许对enum类型和变量进行前向声明。此外,编译器允许声明不完整enum类型的变量。编译器总是假定不完整enum类型的大小和范围与当前平台上的int类型相同。 以下是两行无效代码示例,如果使用-features=extensions选项,可对其进行编译。 enum E; // invalid: forward declaration of enum n...
Besides that it is not needed, it is not proper C, and Clang warns that "forward references to 'enum' types are a Microsoft extension" (-Wmicrosoft-enum-forward-reference). Frankly, I have no idea what that forward declaration is supposed to do, and that this apparently hasn't come up...
// forward declaration enum class Status; // use of fwd-declared enum void continueProcessing(Status s); 2) 潜在类型 enum class 的潜在类型 (underlying type),缺省为 int 型,当然也可自定义潜在类型。无论哪种方式,编译器都会预先知道枚举成员的大小 // underlying type is int enum class Status; ...
代码下载地址 https:///xueweiguo/OOThinking/tree/master/20180603%20ForwardDeclarationOfEnum/ForwardDeclarationOfEnum 觉得本文有帮助?请分享给更多人。 阅读更多更新文章,请扫描下面二维码,关注微信公众号【面向对象思考】
On 64-bit architectures, it is possible for anenumto require a size that is larger than typeint. If that is the case, and if the forward declaration and the definition are visible in the same compilation, the compiler will emit an error. If the actual size is not the assumed size and...
// forward declaration enum class Status; // use of fwd-declared enum void continueProcessing(Status s); 2) 潜在类型enum class 的潜在类型 (underlying type),缺省为 int 型,当然也可自定义潜在类型。无论哪种方式,编译器都会预先知道枚举成员的大小1...
答:如果一个变量只有几种可能的值,那么就可以定义为枚举类型,比如:性别只有男和女,那么就可以将性别定义为一种枚举类型,其中男和女就是性别所包含的变量。所谓”枚举”是指将变量的值一一列举出来,变量的值只能在列举出来的值的范围内。在C++中,枚举类型分为不限定作用域(enum)和限定作用域(enum class)。
// forward declaration enum class Status; // use of fwd-declared enum void continueProcessing(Status s); 1. 2. 3. 4. 5. 2) 潜在类型 enum class 的潜在类型 (underlying type),缺省为 int 型,当然也可自定义潜在类型。无论哪种方式,编译器都会预先知道枚举成员的大小 // underlying type is int...
3. the underlying type of an enum cannot be specified, causing confusion,compatibility problems, and makes forward declaration impossible. The new enums are "enum class" because they combine aspects of traditional enumerations (names values) with aspects of classes (scoped members and absense of ...