4

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.

yotabyte
  • 178
  • 1
  • 12

1 Answers1

9

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

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • Plus you can specify the size of the enum in `C++`. So let's say you want the underlying integral type to be an 8bit number you can say: enum class Myenum : uint8_t{ } – Davide Spataro Jul 12 '17 at 11:20
  • @DavideSpataro: Right but, as far as I can tell, that does not guarantee `sizeof(Myenum) == sizeof(uint8_t)`. I would expect it in general, and it's what I get on my compiler. But be careful about making assumptions. – Lightness Races in Orbit Jul 12 '17 at 11:21
  • @LightnessRacesinOrbit - Specifying the underlying type is for ABI compatibility in forward declarations, is it not? Can we really not assume the size is the same? Because if we can't, it sounds like a defect. – StoryTeller - Unslander Monica Jul 12 '17 at 11:23
  • @StoryTeller: Specifying the underlying type is for specifying range. A C++ program describes the _meaning_ of a program, not what computer instructions are required to implement it. Although the ABI for your platform certainly imposes layout constraints on such a type, C++ itself evidently does not. That's not a defect — it's completely normal. Consider for example that `sizeof()` is also implementation-defined, consider class padding, consider endianness... – Lightness Races in Orbit Jul 12 '17 at 11:25
  • @LightnessRacesinOrbit Yes, but aren't type as uint8_t forced by the standard to have certain properties, as a fixed number of bits? – Davide Spataro Jul 12 '17 at 11:31
  • 1
    @DavideSpataro - If you read the standard, you'll see that like Lightness specified, it only deals with the values an enumeration can take WRT it's underlying type. Nothing about its size. It could theoretically be the size of an int, but take no value larger than 255 without making your program ill-formed. – StoryTeller - Unslander Monica Jul 12 '17 at 11:35
  • @DavideSpataro: A `uint8_t` is not the same as an enumeration with `uint8_t` as underlying type. (Indeed, that's the entire purpose of a scoped enum! hehe) – Lightness Races in Orbit Jul 12 '17 at 11:49
  • Had time to reconsider, and the whole "underlying type doesn't imply object size" seems very wrong given the introduction of [`std::byte`](http://eel.is/c++draft/support.types) as an enum class. Unless the intention of adding `std::byte` has nothing to do with accessing raw memory. – StoryTeller - Unslander Monica Jul 16 '17 at 07:11