lmi
[Top][All Lists]
Advanced

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

[lmi] Arithmetic on enums


From: Greg Chicares
Subject: [lmi] Arithmetic on enums
Date: Mon, 8 Nov 2021 18:03:46 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0

Vadim--debian "bookworm" has upgraded its g++ to version 11, and
now, in some proprietary code whose sole purpose is to generate
product-database files, diagnostics like this occur:

/opt/lmi/src/lmi/../products/src/my_db.cpp:3998:39: error: arithmetic between 
different enume
ration types 'mcenum_uw_basis' and 'enum_database_dimensions' is deprecated 
[-Werror=deprecat
ed-enum-enum-conversion]
 3998 |     v[mce_s_FL + mce_simplified_issue * e_max_dim_state] = 
REDACTED::policy_form_REDACTED_FL_SimpUw;
      |                  ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

Here, 'mce_s_FL' and 'mce_simplified_issue' are particular
enumerators in the 'mcenum_state' and 'mcenum_uw_basis'
enumerations, but 'e_max_dim_state' is just this collection
of related constants representing lengths of database axes
(i.e., array-index strides):

enum enum_database_dimensions
    {e_number_of_axes    = 1 + number_of_indices
    ,e_max_dim_gender    =   3
    ,e_max_dim_uw_class  =   4
    ,e_max_dim_smoking   =   3
    ,e_max_dim_issue_age = 100
    ,e_max_dim_uw_basis  =   5
    ,e_max_dim_state     =  53
    ,e_max_dim_duration  = 100
    };

Of course I don't want to suppress the compiler diagnostic
with a '-Wno-whatever' option. The diagnostic goes away if I
replace 'e_max_dim_state' with its numeric value:

- mce_s_FL + mce_simplified_issue * e_max_dim_state
+ mce_s_FL + mce_simplified_issue * 53

so my first thought is to replace the enum above with:
  constexpr int e_max_dim_state {53};
and so on. That seems righteous to me: whereas 'mcenum_state'
is a genuine type that could be used for a function argument,
'enum_database_dimensions' isn't never used in any such way:
it's just a set of constants that are grouped together for
convenience, and the enum might as well be anonymous.

But am I missing something? I had guessed that the expression
  mce_s_FL + mce_simplified_issue * 53
would still elicit the same diagnostic, but at '+' instead
of at '*', because the types differ:

  mcenum_state  +    mcenum_uw_basis    *  int
  mce_s_FL      + mce_simplified_issue  *  53
                ^ here's where I expected a diagnostic

and addition is as surely "arithmetic" as is multiplication.
Is the long-term vision of C++ to deprecate all arithmetic
on enums?


reply via email to

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