在C ++中,在“枚举类”和“枚举类”之间存储器使用上有差别。对“enum”? [英] In C++, is there a difference in memory usage between "enum class" versus "enum"?

查看:108
本文介绍了在C ++中,在“枚举类”和“枚举类”之间存储器使用上有差别。对“enum”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解使用强类型枚举类而不是枚举的好处,但同学和我想知道一个或另一个是否占用更多的内存。

解决方案

枚举类仍然只是整数值,没有额外的开销。然而,枚举类的底层类型默认为 int 而常规枚举类型是未指定的,实现可以自由选择最小整数类型。所以在理论上如果你没有指定底层类型,一个枚举类可能比一个简单的 enum / p>

C ++规格7.2.5 - 枚举减速



枚举类


底层类型可以使用enum-base明确指定;如果未明确指定,则作用域枚举类型的底层类型为 int


enum


如果没有为第一个枚举器指定初始化器,值具有未指定的整数
类型。



I understand the benefits of using the strongly typed "enum class" over "enum", but a classmate and I are wondering if one or the other takes up more memory.

解决方案

enum classs are still just integral values with no additional overhead. However an enum class's underlying type defaults to int while regular enum the type is unspecified and the implementation is free to select a minimal integral type. So in theory if you don't specify the underlying type an enum class may be bigger than a plain enum.

C++ specification 7.2.5 - Enumeration Deceleration

enum class

The underlying type can be explicitly specified using enum-base; if not explicitly specified, the underlying type of a scoped enumeration type is int.

enum

If no initializer is specified for the first enumerator, the initializing value has an unspecified integral type.

这篇关于在C ++中,在“枚举类”和“枚举类”之间存储器使用上有差别。对“enum”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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