Why is `std::byte` an enum class instead of a class?

后端 未结 1 2052
旧巷少年郎
旧巷少年郎 2021-02-07 05:40

std::byte is an abstraction that is supposed to provide a type safe(r) access to regions of memory in C++, starting with the new standard 17. However, it\'s declare

相关标签:
1条回答
  • 2021-02-07 06:17

    A class with an unsigned char member would not be required by the standard to be the same size or alignment as unsigned char. Whereas the standard requires that enumerations be the same size and alignment as their underlying types.

    Now, the standard could have simply declared that it is a class type with no standard-defined member, but with specific requirements on its size, alignment, constexpr constructors, etc; implementations would have to follow through on those expectations. But it's much easier to simply use enum class to get the same effect. You get all of the constructors and conversion behavior that you'd expect. And since enum class types are treated as different types than their underlying type, you get all of the behavior you want with no real downside to defining it this way.

    0 讨论(0)
提交回复
热议问题