Enumeration based on integer boolean conversion

In my compiler project, I have an enumeration that looks like

enum Result { No, Maybe, Yes }; 

I put No explicitly in the first position, so that I can rely on a Boolean evaluation of false . If my compiler is unsure of something and needs to wait for the facts to complete, its analysis functions will return Maybe . Used as

 if(!typesEqual(t1, t2)) { diagnose(types_unequal) << t1 << t2; } 

I wonder if you or your company consider it a bad style not to compare explicitly with No

 if(typesEqual(t1, t2) == No) { /* ... */ } 

The comparison clearly seems verbose to me, but relying on an implicit logical transformation somehow makes me feel guilty. Did you have this feeling before and how did you deal with it?

+4
source share
4 answers

I would also be to blame for this, because after reading the above code, what would you expect the expression boolean typesEqual() return for Maybe ? Is this true? May be! Will he return the lie? May be! We do not know what the whole item is listed. Therefore, it makes sense to explicitly compare with No , although it is more detailed.

+5
source

I usually do not use explicit comparison with zero for integer or pointer types, since the conversion to Boolean is clear and obvious.

However, I always use explicit comparison for enumerations, in case someone changes the definition of an enumeration. You can never be sure that someone will not change the listing later, after all.

Relying on the base numerical value of the counter just seems bad.

+3
source

This is similar to Boost.Tribool . Tribool supports conversion to bool for use in conditional statements, which seems to suggest that implicit conversion to bool in a case, for example, is not bad, at least according to the Boost organization (which I find pretty reasonable).

+1
source

I do not like that it is asymmetric.

eg. what you really wanted was

 if(typesEqual(t1, t2) == Yes) { do_something(); } 

but by chance you wrote

 if(typesEqual(t1, t2)) { do_something(); } 

It seems strange / ugly that you can use the logical trick for No, but not for Yes.

I think I would solve this by renaming the function to tryCompareTypes(t1, t2) and changing your enum to

 enum Result { Maybe, No, Yes }; 

So tryCompareTypes() returns 0 if "it was not possible" to finally decide whether the types are equal, otherwise it returns either "No" or "Yes", both of which are non-zero and, therefore, indicate "success".

0
source

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


All Articles