Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are c++ enum structs bigger in size compared to regular enums?

Tags:

c++

enums

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.

like image 991
yotabyte Avatar asked Jan 04 '23 18:01

yotabyte


1 Answers

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]).

like image 67
Lightness Races in Orbit Avatar answered Feb 16 '23 23:02

Lightness Races in Orbit