Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to avoid anonymous enumeration in C++

Shulou Source: shulou.com Published: 2022-06-02 07:43:00 11月22日 Update

C++ how to avoid anonymous enumeration, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Reason (reason)

If you can't name an enumeration, the values are not related

If you cannot name enumerations, there is no association between these values.

Example, bad (negative example)

Enum {red = 0xFF0000, scale = 4, is_signed = 1}

Such code is not uncommon in code written before there were convenient alternative ways of specifying integer constants.

Before convenient candidates for defining shaping constants existed, such code was not uncommon.

Alternative (optional)

Use constexpr values instead. For example:

Use constant expressions. For example:

Constexpr int red = 0xFF0000

Constexpr short scale = 4

Constexpr bool is_signed = true; after reading the above, have you mastered how to avoid anonymous enumerations in C++? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Clocking content cause constant method more problem rare helpless to this end between code negative to this skills ways examples articles experience industry Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Tech Info Huawei Shulou Technology Xiaomi