lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [lmi] Arithmetic on enums in C++20


From: Greg Chicares
Subject: Re: [lmi] Arithmetic on enums in C++20
Date: Mon, 8 Nov 2021 22:47:58 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0

On 11/8/21 9:45 PM, Vadim Zeitlin wrote:
> On Mon, 8 Nov 2021 18:03:46 +0000 Greg Chicares <gchicares@sbcglobal.net> 
> wrote:
[...]
> GC> But am I missing something? I had guessed that the expression
> GC>   mce_s_FL + mce_simplified_issue * 53
> GC> would still elicit the same diagnostic, but at '+' instead
> GC> of at '*', because the types differ:
> GC> 
> GC>   mcenum_state  +    mcenum_uw_basis    *  int
> GC>   mce_s_FL      + mce_simplified_issue  *  53
> GC>                 ^ here's where I expected a diagnostic
> GC> 
> GC> and addition is as surely "arithmetic" as is multiplication.
> 
>  I haven't tested this, but I am almost sure that the reason you don't get
> the warning here is that the result of multiplying an enum by an int is an
> int, due to the usual type promotion rules, and adding enums and ints is
> still allowed.

Ah, of course. They have deprecated arithmetic on enums, but not
promotion from classic "unscoped" enums to integers. Thanks for
explaining what I had missed.

> If you removed this "* 53", you would almost certainly get
> the warning you expect.

Indeed:

error: arithmetic between different enumeration types ‘mcenum_state’ and 
‘mcenum_uw_basis’ is deprecated [-Werror=deprecated-enum-enum-conversion]
  196 |     std::cout << mce_s_FL + mce_simplified_issue << std::endl;
      |                  ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~

> GC> Is the long-term vision of C++ to deprecate all arithmetic
> GC> on enums?
> 
>  I believe all of it is already deprecated. Whether it will be actually
> removed is less clear, but considering that just about non-trivial code
> base which I tried compiling with C++20 compilers had at least some
> instances of this warning, it probably won't happen any time soon.

In the language of diplomacy:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2139r1.html#3.1
| There is an ongoing concern of gaining more specific feedback on
| implementation experience with the deprecation warning before
| making any change



reply via email to

[Prev in Thread] Current Thread [Next in Thread]