Does C ++ have casting to list problems?

Given the following code:

enum Options { Surf = 0x01, Out = 0x02 }; Options all = (Options) ( Surf | Out); 
  • Does this casting have problems?
  • If this casting makes sense, then why?

Based on my understanding, options define only two variables. How does 0x03 matter?

+4
source share
5 answers

Does this casting have problems?

No.

If this casting makes sense, then why? Based on my understanding, options define only two variables, how does the value 0x03 make sense?

The Options enumeration type has two named enumerations, but the range of values ​​it represents is large enough to be used as a bit field containing each of the counters.

In short: yes, it is valid and clearly defined to use an enumeration for a bit field like this.

As stated in the comments on this answer, the official language allowing this can be found in the C ++ standard (C ++ 03 7.2 / 6):

For an enumeration, where e min is the smallest enumerator and e max is the largest, the enumeration values ​​are the values ​​of the base type in the range b min to b max , where b min and b max are the smallest and largest values, respectively, the smallest bit field that can store e min and e max .

You can define an enumeration that has values ​​that are not defined by any of its counters.

There is some debate as to whether this style is good. Of course, it can be argued that it is often assumed that an enumeration object can store only one of its counters, so this code can be confusing and error prone.

On the other hand, I would say that this is usually quite obvious when an enumeration is intended to be used as a bit field. Typically, such an enumeration is called the suffix Options or Flags or something similar. Similarly, if each of the counters has a given value, which is clearly the only bit in the set, this enumeration is probably intended to be used as a bit field.

+7
source

Q1> Yes. You use enumeration when it seems to you that a bitmask is required.

Q2> You are right, it does not make sense. Using enum implies that the variable all will be equal to one of the elements in Options, and not the ordd combination of several elements.

+1
source

Besides some syntax issues, your code really works. But it's sticky. Enums are ints, but in style, you should stick to the values ​​that are actually enumerated.

Maybe use the int bit for a bitwise collection. and be careful to assign powers 2, I think. If you really want to hit.

 typedef enum Options { Surf = 0x01, Out = 0x02 } Options; 
0
source

enums are just placeholders for int values, so when you are bitwise or you get this: in binary (4-bit representation, so this is easier for me): 0001 | 0010 = 0011 or 3. Enumerations can be any value, even completely invalid.

0
source

You may need to assign an arbitrary integer value to a variable of type enum , but this is a bad style. As always, the code for readability is first, and the second is the compiler. The enum variable is expected to contain only valid enumeration constants.

I would change the last line to:

 int all = ( Surf | Out); 
0
source

Source: https://habr.com/ru/post/1335610/


All Articles