与常规枚举相比,c ++枚举结构的大小是否更大? [英] Are c++ enum structs bigger in size compared to regular enums?

查看:129
本文介绍了与常规枚举相比,c ++枚举结构的大小是否更大?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与常规枚举相比,C ++枚举结构(类)的大小是否更大?也就是说,假设它们枚举了相同的精确数据,那么什么转化为更多字节的指令代码?



我正在嵌入式环境中开发,这个问题很重要。 Id'e喜欢使用枚举结构允许的类型安全性和范围限制,而不是费用代码膨胀。

解决方案



语义的差异由编译器管理,因为它只涉及类型系统。



没有理由需要更多的存储空间,或者需要更多的指示。



您可以轻松地查看前者使用 sizeof



但是,我应该注意到C ++不保证任何这种类型的布局,除了说共享相同底层类型的两个枚举类型是layout-compatible( [C ++ 14:7.2 / 9] )。


Are C++ enum structs (class) bigger in size compared to regular enums? i.e. what translates to more bytes of instruction code assuming they enumerate the same exact data?

I am developing in an embedded environment and this issue is kinda important. Id'e like to use the type-safety and scoping that enum structs allow, but not on the expense of code bloat.

解决方案

No.

The difference in semantics is managed by the compiler, as it relates only to the type system.

There is no reason for more storage to be required, or for more instructions to be required.

You could easily check out the former on your actual types, using sizeof.

However, I should note that C++ doesn't guarantee the layout of any such type, except to say that two enumeration types sharing the same underlying type are "layout-compatible" ([C++14: 7.2/9]).

这篇关于与常规枚举相比,c ++枚举结构的大小是否更大?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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